forked from mirror/rarfilesource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFS.cpp
832 lines (686 loc) · 19.1 KB
/
RFS.cpp
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
/*
* Copyright (C) 2008-2012, OctaneSnail <os@v12pwr.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <streams.h>
#include <initguid.h>
#include <strsafe.h>
#include "RFS.h"
#include "Utils.h"
#include "RAR.h"
#include "Anchor.h"
#include "resource.h"
#include "Mediatype.h"
#include "File.h"
// {9FFE11D2-29F2-463f-AD5F-C04A5EE2E58D}
DEFINE_GUID(CLSID_RARFileSource,
0x9ffe11d2, 0x29f2, 0x463f, 0xad, 0x5f, 0xc0, 0x4a, 0x5e, 0xe2, 0xe5, 0x8d);
#define RFS_GUID_STRING "{9FFE11D2-29F2-463f-AD5F-C04A5EE2E58D}"
//{1AC0BEBD-4D2B-45AD-BCEB-F2C41C5E3788}
DEFINE_GUID(MEDIASUBTYPE_Matroska,
0x1AC0BEBD, 0x4D2B, 0x45AD, 0xBC, 0xEB, 0xF2, 0xC4, 0x1C, 0x5E, 0x37, 0x88);
// Setup information
const AMOVIESETUP_MEDIATYPE sudPinTypes =
{
&MEDIATYPE_Stream, // Major type
&MEDIASUBTYPE_NULL // Minor type
};
const AMOVIESETUP_PIN sudpPin =
{
L"Output", // Pin name
FALSE, // Is it rendered
TRUE, // Is it an output
FALSE, // Zero instances allowed
FALSE, // Multiple instances allowed
&CLSID_NULL, // Connects to filter
NULL, // Connects to pin
1, // Number of types
&sudPinTypes // Pin types
};
const AMOVIESETUP_FILTER sudRARFileSource =
{
&CLSID_RARFileSource, // Filter CLSID
L"RAR File Source", // Filter name
MERIT_UNLIKELY, // Filter merit
1, // Number of pins
&sudpPin // Pin information
};
// List of class IDs and creator functions for the class factory.
CFactoryTemplate g_Templates [] =
{
{
L"RAR File Source",
&CLSID_RARFileSource,
CRARFileSource::CreateInstance,
NULL,
&sudRARFileSource
}
};
int g_cTemplates = sizeof (g_Templates) / sizeof (g_Templates [0]);
/* static */
file_type_t CRARFileSource::s_file_types [] =
{
{ "avi", &MEDIASUBTYPE_Avi },
{ "mpg", &MEDIASUBTYPE_MPEG1System },
{ "vob", &MEDIASUBTYPE_MPEG2_PROGRAM },
{ "mkv", &MEDIASUBTYPE_Matroska },
{ "mka", &MEDIASUBTYPE_Matroska },
{ "mks", &MEDIASUBTYPE_Matroska },
{ "mov", &MEDIASUBTYPE_QTMovie },
{ "wav", &MEDIASUBTYPE_WAVE },
{ "mp3", &MEDIASUBTYPE_MPEG1Audio },
{ "mpa", &MEDIASUBTYPE_MPEG1Audio },
{ "mpv", &MEDIASUBTYPE_MPEG1Video },
{ "dat", &MEDIASUBTYPE_MPEG1VideoCD },
{ NULL, NULL }
};
extern "C" BOOL WINAPI DllEntryPoint (HINSTANCE, ULONG, LPVOID);
BOOL WINAPI DllMain (HINSTANCE hDllHandle, DWORD dwReason, LPVOID lpReserved)
{
return DllEntryPoint (hDllHandle, dwReason, lpReserved);
}
STDAPI DllRegisterServer ()
{
HKEY key;
LONG ret;
int i;
wchar_t key_name [] = L"Media Type\\Extensions\\.rar";
ret = RegCreateKey (HKEY_CLASSES_ROOT, key_name, &key);
if (ret != ERROR_SUCCESS)
{
ret = RegOpenKey (HKEY_CLASSES_ROOT, key_name, &key);
if (ret != ERROR_SUCCESS)
return ret;
}
ret = RegSetValueExA (key, "Source Filter", 0, REG_SZ, (BYTE *) RFS_GUID_STRING, (DWORD) strlen (RFS_GUID_STRING) + 1);
if (ret != ERROR_SUCCESS)
{
RegCloseKey (key);
return ret;
}
RegCloseKey (key);
for (i = 0; i < 100; i ++)
{
key_name [24] = L'0' + i / 10;
key_name [25] = L'0' + i % 10;
ret = RegCreateKey (HKEY_CLASSES_ROOT, key_name, &key);
if (ret != ERROR_SUCCESS)
{
ret = RegOpenKey (HKEY_CLASSES_ROOT, key_name, &key);
if (ret != ERROR_SUCCESS)
continue;
}
ret = RegSetValueExA (key, "Source Filter", 0, REG_SZ, (BYTE *) RFS_GUID_STRING, (DWORD) strlen (RFS_GUID_STRING) + 1);
RegCloseKey (key);
}
return AMovieDllRegisterServer2 (TRUE);
}
STDAPI DllUnregisterServer ()
{
HKEY key;
LONG ret;
char value [40];
DWORD len = sizeof (value);
wchar_t key_name [] = L"Media Type\\Extensions\\.rar";
int i;
for (i = 0; i < 101; i ++)
{
ret = RegOpenKey (HKEY_CLASSES_ROOT, key_name, &key);
if (ret == ERROR_SUCCESS)
{
ret = RegQueryValueExA (key, "Source Filter", NULL, NULL, (BYTE *) value, &len);
RegCloseKey (key);
if (ret == ERROR_SUCCESS)
{
if (!_stricmp (value, RFS_GUID_STRING))
RegDeleteKey (HKEY_CLASSES_ROOT, key_name);
}
}
key_name [24] = L'0' + i / 10;
key_name [25] = L'0' + i % 10;
}
return AMovieDllRegisterServer2 (FALSE);
}
CRARFileSource::CRARFileSource (LPUNKNOWN punk, HRESULT *phr) :
CBaseFilter (L"RAR File Source", punk, &m_lock, CLSID_RARFileSource),
m_pin (this, &m_lock, phr),
m_file_name (NULL),
m_file (NULL)
{
if (phr)
*phr = S_OK;
}
CRARFileSource::~CRARFileSource ()
{
delete m_file_name;
delete m_file;
}
CUnknown *CRARFileSource::CreateInstance (LPUNKNOWN punk, HRESULT *phr) // OK
{
CRARFileSource *pNewObject = new CRARFileSource (punk, phr);
if (pNewObject == NULL)
*phr = E_OUTOFMEMORY;
DbgSetModuleLevel (LOG_TRACE, 2);
DbgLog((LOG_TRACE, 2, L"CRARFileSource::CreateInstance () succeded."));
return pNewObject;
}
STDMETHODIMP CRARFileSource::NonDelegatingQueryInterface (REFIID riid, void **ppv)
{
if (riid == IID_IFileSourceFilter)
return GetInterface ((IFileSourceFilter *) this, ppv);
else
return CBaseFilter::NonDelegatingQueryInterface (riid, ppv);
}
/* static */
void CRARFileSource::UpdateArchiveName (wchar_t *ext, size_t len, int volume, bool new_numbering)
{
if (new_numbering)
{
StringCchPrintf (ext, len + 1, L"%0*d", len, volume + 1);
ext [len] = L'.';
}
else
{
ASSERT (len == 2);
if (volume == 0)
{
ext [0] = L'a';
ext [1] = L'r';
}
else
StringCchPrintf (ext - 1, len + 2, L"%c%02d", 114 + (volume - 1) / 100, (volume - 1) % 100);
}
}
#define HEADER_SKIP_FILE \
delete [] rh.fh.filename; \
SetFilePointerEx (hFile, rh.bytesRemaining, NULL, FILE_CURRENT); \
continue;
int CRARFileSource::ScanArchive (wchar_t *archive_name, List<File> *file_list, int *ok_files_found)
{
DWORD dwBytesRead;
char *filename = NULL;
wchar_t *current_rar_filename = NULL, *rar_ext;
bool first_archive_file = true, rewind;
bool multi_volume = false, new_numbering = false;
rar_header_t rh;
BYTE marker [7];
BYTE expected [7] = { 0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00 };
FilePart *new_part, *prev_part;
LONGLONG collected;
DWORD ret;
DWORD files = 0, volumes = 0;
int volume_digits;
File *file = NULL;
*ok_files_found = 0;
LARGE_INTEGER zero = {0};
MediaType *mType;
List<MediaType> mediaTypeList (true);
Anchor<File> af (&file);
ArrayAnchor<wchar_t> acrf (¤t_rar_filename);
HANDLE hFile = INVALID_HANDLE_VALUE;
Anchor<HANDLE> ha (&hFile);
int cch = lstrlen (archive_name) + 1;
current_rar_filename = new wchar_t [cch];
if (!current_rar_filename)
{
ErrorMsg (0, L"Out of memory.");
return 0;
}
CopyMemory (current_rar_filename, archive_name, cch * sizeof (wchar_t));
rar_ext = wcsrchr (current_rar_filename, '.');
if (getMediaTypeList (&mediaTypeList) == -1)
return 0; // this means out of memory
// Scan through archive volume(s)
while (true)
{
ha.Close ();
DbgLog((LOG_TRACE, 2, L"Loading file \"%s\".", current_rar_filename));
hFile = CreateFile (current_rar_filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
if (first_archive_file || rewind)
{
ErrorMsg (GetLastError (), L"Could not open file \"%s\".", current_rar_filename);
return files;
}
else
break;
}
rewind = false;
// Read marker.
if (!ReadFile (hFile, marker, 7, &dwBytesRead, NULL) || dwBytesRead != 7)
{
ErrorMsg (GetLastError (), L"Could not read RAR header.");
return files;
}
if (memcmp (marker, expected, 7))
{
ErrorMsg (0, L"Incorrect RAR marker.");
return files;
}
// Read archive header.
if (ReadHeader (hFile, &rh))
return files;
LOG_HEADER(&rh);
if (rh.ch.type != HEADER_TYPE_ARCHIVE)
{
ErrorMsg (0, L"Unexpected RAR header type.");
return files;
}
if (rh.ch.flags & MHD_PASSWORD)
{
ErrorMsg (0, L"Encrypted RAR volumes are not supported.");
return files;
}
if (first_archive_file)
{
new_numbering = !!(rh.ch.flags & MHD_NEWNUMBERING);
multi_volume = !!(rh.ch.flags & MHD_VOLUME);
if (multi_volume)
{
if (!rar_ext)
{
ErrorMsg (0, L"Input file does not end with .rar");
return files;
}
// Locate volume counter
if (new_numbering)
{
volume_digits = 0;
do
{
rar_ext --;
volume_digits ++;
} while (iswdigit (*(rar_ext - 1)));
}
else
{
rar_ext += 2;
volume_digits = 2;
}
if (!(rh.ch.flags & MHD_FIRSTVOLUME))
{
DbgLog((LOG_TRACE, 2, L"Rewinding to the first file in the set."));
UpdateArchiveName (rar_ext, volume_digits, volumes, new_numbering);
first_archive_file = false;
rewind = true;
continue;
}
}
}
else
{
ASSERT (new_numbering == !!(rh.ch.flags & MHD_NEWNUMBERING));
ASSERT (rh.ch.flags & MHD_VOLUME);
}
// Find file headers
while (true)
{
// Read file header.
if (ret = ReadHeader (hFile, &rh))
{
if (ret == ERROR_HANDLE_EOF)
break;
else
return files;
}
LOG_HEADER(&rh);
if (rh.ch.type == HEADER_TYPE_END)
{
// TODO: Verify that the volume number in the header matches our volume counter.
// Check for EARC_NEXT_VOLUME removed as this flag is missing on some archives.
break;
}
if (rh.ch.type != HEADER_TYPE_FILE)
{
SetFilePointerEx (hFile, rh.bytesRemaining, NULL, FILE_CURRENT);
DbgLog((LOG_TRACE, 2, L"Skipping unknown header of type %02x.", rh.ch.type));
continue;
}
DbgLog((LOG_TRACE, 2, L"SIZE %08lx %08lx OS %02x CRC %08lx TIMESTAMP %08lx VERSION %d METHOD %02x LEN %04lx ATTRIB %08lx",
rh.fh.size.HighPart, rh.fh.size.LowPart, rh.fh.os, rh.fh.crc, rh.fh.timestamp, rh.fh.version, rh.fh.method, rh.fh.name_len, rh.fh.attributes));
DbgLog((LOG_TRACE, 2, L"FILENAME \"%S\"", rh.fh.filename));
if ((rh.ch.flags & LHD_WINDOWMASK) == LHD_DIRECTORY)
{
DbgLog((LOG_TRACE, 2, L"Skipping directory."));
HEADER_SKIP_FILE
}
if (filename)
{
if (strcmp (filename, rh.fh.filename))
{
// TODO: We should probably dump the old file start fresh with this one
// since the lazy error handling at other places may cause us to end up here.
DbgLog((LOG_TRACE, 2, L"Incorrect file found."));
HEADER_SKIP_FILE
}
if (!(rh.ch.flags & LHD_SPLIT_BEFORE))
{
// TODO: Decide if it's better to ignore the missing flag.
DbgLog((LOG_TRACE, 2, L"LHD_SPLIT_BEFORE flag was not set as expected."));
HEADER_SKIP_FILE
}
delete [] rh.fh.filename;
}
else
{
if (!multi_volume && rh.ch.flags & (LHD_SPLIT_BEFORE | LHD_SPLIT_AFTER))
{
ErrorMsg (0, L"Split file in a single volume archive.");
delete [] rh.fh.filename;
return files;
}
if (rh.ch.flags & LHD_SPLIT_BEFORE)
{
if (first_archive_file)
{
// Some archives incorrectly have MHD_FIRSTVOLUME set on all files, attempt rewind.
DbgLog((LOG_TRACE, 2, L"Rewinding to the first file in the set."));
UpdateArchiveName (rar_ext, volume_digits, volumes, new_numbering);
rewind = true;
delete [] rh.fh.filename;
break;
}
else
{
// TODO: Decide if it's better to just abort the entire scanning process here.
DbgLog((LOG_TRACE, 2, L"Not at the beginning of the file."));
HEADER_SKIP_FILE
}
}
files ++;
collected = 0;
ASSERT (!file);
file = new File ();
if (!file)
{
ErrorMsg (0, L"Out of memory.");
delete [] rh.fh.filename;
return files;
}
file->media_type.SetType (&MEDIATYPE_Stream);
file->media_type.SetSubtype (&MEDIASUBTYPE_NULL);
file->filename = rh.fh.filename;
file->size = rh.fh.size.QuadPart;
filename = rh.fh.filename;
if (rh.ch.flags & LHD_PASSWORD)
{
DbgLog((LOG_TRACE, 2, L"Encrypted files are not supported."));
file->unsupported = true;
}
if (rh.fh.method != 0x30)
{
DbgLog((LOG_TRACE, 2, L"Compressed files are not supported."));
file->unsupported = true;
}
}
if (!file->unsupported)
{
new_part = new FilePart ();
if (!new_part)
{
ErrorMsg (0, L"Out of memory.");
return files;
}
// Is this the 1st part?
if (!file->parts)
{
file->parts = 1;
file->list = new_part;
}
else
{
file->parts ++;
prev_part->next = new_part;
}
prev_part = new_part;
SetFilePointerEx (hFile, zero, (LARGE_INTEGER*) &new_part->in_rar_offset, FILE_CURRENT);
new_part->in_file_offset = collected;
new_part->size = rh.bytesRemaining.QuadPart;
new_part->file = CreateFile (current_rar_filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (new_part->file == INVALID_HANDLE_VALUE)
{
ErrorMsg (GetLastError (), L"Could not open file \"%s\".", current_rar_filename);
return files;
}
}
collected += rh.bytesRemaining.QuadPart;
SetFilePointerEx (hFile, rh.bytesRemaining, NULL, FILE_CURRENT);
// Is file complete?
if (!(rh.ch.flags & LHD_SPLIT_AFTER))
{
if (!file->unsupported && file->size != collected)
DbgLog((LOG_TRACE, 2, L"The file is not the sum of it's parts. expected = %lld, actual = %lld", file->size, collected));
if (file->parts)
{
file->array = new FilePart [file->parts];
if (!file->array)
{
ErrorMsg (0, L"Out of memory.");
return files;
}
FilePart *fp = file->list;
file->list = NULL;
for (int i = 0; i < file->parts; i ++)
{
FilePart *tmp;
memcpy (file->array + i, fp, sizeof (FilePart));
tmp = fp;
fp = fp->next;
tmp->file = INVALID_HANDLE_VALUE;
delete tmp;
}
}
if (!file->unsupported)
{
if (!checkFileForMediaType (file, &mediaTypeList, &mType))
return files; // this means out of memory
if (mType)
{
#ifdef _DEBUG
LPOLESTR majorType, subType;
StringFromCLSID (mType->majorType, &majorType);
StringFromCLSID (mType->subType, &subType);
DbgLog((LOG_TRACE, 2, L"Filetype detection determined:"));
DbgLog((LOG_TRACE, 2, L" Major type: %s", majorType));
DbgLog((LOG_TRACE, 2, L" Subtype: %s", subType));
CoTaskMemFree (majorType);
CoTaskMemFree (subType);
#endif
file->media_type.SetType (&mType->majorType);
file->media_type.SetSubtype (&mType->subType);
file->type_known = true;
(*ok_files_found) ++;
}
}
// TODO: Figure out if checking file extensions is necessary anymore.
if (!file->type_known)
{
char *ext = strrchr (file->filename, '.');
if (ext)
{
ext ++;
int i;
for (i = 0; s_file_types [i].extension; i ++)
{
if (!_stricmp (ext, s_file_types [i].extension))
break;
}
if (s_file_types [i].extension)
{
file->media_type.SetSubtype (s_file_types [i].guid);
file->type_known = true;
if (!file->unsupported)
(*ok_files_found) ++;
}
else
{
file->unsupported = true;
DbgLog((LOG_TRACE, 2, L"Unknown file extension."));
}
}
else
{
file->unsupported = true;
DbgLog((LOG_TRACE, 2, L"No file extension."));
}
}
if (filename != file->filename)
delete filename;
filename = NULL;
file_list->InsertLast (file);
file = NULL;
}
}
first_archive_file = false;
if (rewind)
continue;
if (!multi_volume)
break;
// Continue to the next volume.
volumes ++;
UpdateArchiveName (rar_ext, volume_digits, volumes, new_numbering);
}
if (file)
{
// TODO: Decide if we should allow playback of truncated files.
ErrorMsg (0, L"Couldn't find all archive volumes.");
}
return files;
}
/* static */
INT_PTR CALLBACK CRARFileSource::DlgFileList (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int index;
switch (uMsg)
{
case WM_INITDIALOG:
{
int len;
List<File> *file_list = (List<File> *) lParam;
File *file = file_list->First ();
wchar_t *tempString;
while (file)
{
if (!file->unsupported)
{
len = (int) strlen (file->filename) + 1;
tempString = new wchar_t [len];
MultiByteToWideChar (CP_ACP, 0, file->filename, -1, tempString, len);
index = ListBox_AddString (GetDlgItem (hwndDlg, IDC_FILELIST), tempString);
ListBox_SetItemData (GetDlgItem (hwndDlg, IDC_FILELIST), index, file);
delete [] tempString;
}
file = file_list->Next (file);
}
ListBox_SetCurSel (GetDlgItem (hwndDlg, IDC_FILELIST), 0);
SetForegroundWindow (hwndDlg);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
index = ListBox_GetCurSel (GetDlgItem (hwndDlg, IDC_FILELIST));
EndDialog (hwndDlg, ListBox_GetItemData (GetDlgItem (hwndDlg, IDC_FILELIST), index));
return TRUE;
case IDCANCEL:
EndDialog (hwndDlg, NULL);
return TRUE;
case IDC_FILELIST:
switch (HIWORD (wParam))
{
case LBN_DBLCLK:
index = ListBox_GetCurSel ((HWND) lParam);
EndDialog (hwndDlg, ListBox_GetItemData ((HWND) lParam, index));
return TRUE;
}
}
break;
case WM_CLOSE:
EndDialog (hwndDlg, NULL);
return TRUE;
}
return FALSE;
}
// IFileSourceFilter methods
STDMETHODIMP CRARFileSource::Load (LPCOLESTR lpwszFileName, const AM_MEDIA_TYPE *pmt)
{
List <File> file_list;
int num_files, num_ok_files;
CAutoLock lck (&m_lock);
if (!lpwszFileName)
return E_POINTER;
if (m_file)
{
DbgLog((LOG_TRACE, 2, L"CRARFileSource::Load called with file already loaded."));
return E_UNEXPECTED;
}
int cch = lstrlen (lpwszFileName) + 1;
if (m_file_name)
delete m_file_name;
m_file_name = new WCHAR [cch];
if (!m_file_name)
{
ErrorMsg (0, L"Out of memory.");
return E_OUTOFMEMORY;
}
CopyMemory (m_file_name, lpwszFileName, cch * sizeof (WCHAR));
num_files = ScanArchive ((wchar_t *) lpwszFileName, &file_list, &num_ok_files);
DbgLog((LOG_TRACE, 2, L"Found %d files out of which %d are media files.", num_files, num_ok_files));
if (!num_ok_files)
{
ErrorMsg (0, L"No media files found in the archive.");
return E_UNEXPECTED; // TODO: Figure out a better error code.
}
if (num_ok_files == 1)
{
m_file = file_list.First ();
while (m_file->unsupported)
m_file = file_list.Next (m_file);
}
else
{
m_file = (File *) DialogBoxParam (g_hInst, MAKEINTRESOURCE(IDD_FILELIST), 0, DlgFileList, (LPARAM) &file_list);
if (!m_file)
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
}
if (pmt)
m_file->media_type = *pmt;
m_file->Unlink ();
m_pin.SetFile (m_file);
file_list.Clear ();
return S_OK;
}
// Behaves like IPersistFile::Load
STDMETHODIMP CRARFileSource::GetCurFile (LPOLESTR *ppszFileName, AM_MEDIA_TYPE *pmt)
{
if (!ppszFileName)
return E_POINTER;
if (m_file_name != NULL)
{
DWORD n = sizeof (WCHAR) * (lstrlen (m_file_name) + 1);
*ppszFileName = (LPOLESTR) CoTaskMemAlloc (n);
if (*ppszFileName != NULL)
CopyMemory (*ppszFileName, m_file_name, n);
else
return E_OUTOFMEMORY;
}
else
*ppszFileName = NULL;
if (pmt != NULL)
CopyMediaType (pmt, &m_file->media_type);
return NOERROR;
}