-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnsoniqFS.c
3904 lines (3408 loc) · 108 KB
/
EnsoniqFS.c
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
//----------------------------------------------------------------------------
// EnsoniqFS plugin for TotalCommander
//
// MAIN MODULE
//----------------------------------------------------------------------------
//
// (c) 2006 Thoralt Franz
//
// This source code was written using Dev-Cpp 4.9.9.2
// If you want to compile it, get Dev-Cpp. Normally the code should compile
// with other IDEs/compilers too (with small modifications), but I did not
// test it.
//
//----------------------------------------------------------------------------
// License
//----------------------------------------------------------------------------
// 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; either version 2
// of the License, or (at your option) any later version.
//
// 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.
// rea
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
// Alternatively, download a copy of the license here:
// http://www.gnu.org/licenses/gpl.txt
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// includes
//----------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <ctype.h>
#include "log.h"
#include "fsplugin.h"
#include "disk.h"
#include "ini.h"
#include "EnsoniqFS.h"
#include "error.h"
#include "cache.h"
#include "optionsdlg.h"
#include "choosediskdlg.h"
#include "resource.h"
#include "string.h"
#include "bank.h"
//----------------------------------------------------------------------------
// globals
//----------------------------------------------------------------------------
// global root of device list
DISK *g_pDiskListRoot = NULL;
// global root find handle list (linked list with all open find handles,
// multiple open find lists for Total Commander are possible, yet unlikely)
FIND_HANDLE *g_pHandleRoot = NULL;
// variables to receive initialization parameters from Total Commander
FsDefaultParamStruct g_DefaultParams;
int g_iPluginNr = 0;
tProgressProc g_pProgressProc;
tLogProc g_pLogProc;
tRequestProc g_pRequestProc;
HINSTANCE g_hInst = 0;
// global options
int g_iOptionEnableFloppy = 1;
int g_iOptionEnableCDROM = 1;
int g_iOptionEnableImages = 1;
int g_iOptionEnablePhysicalDisks = 1;
int g_iOptionAutomaticRescan = 1;
int g_iOptionEnableLogging = 1;
char g_cOptionInstallPath[261];
int g_iOptionBankAdaption = 0;
int g_iOptionBankSourceDevice = -1;
int g_iOptionBankTargetDevice = 0;
// flag for operations on multiple files to flush the cache only once after
// the last file
unsigned char g_ucMultiple = 0;
// synchronization between multiple instances of EnsoniqFS
const char g_cMemoryFN[] = "8EEA3415-4462-4f26-87BE-58ED9B9E5D86-EnsoniqFS";
#define SHARED_MEMORY_SIZE 4096
unsigned char *g_ucSharedMemory = NULL;
HANDLE g_hMemoryMappedFile = INVALID_HANDLE_VALUE;
//----------------------------------------------------------------------------
// upcase
//
// Converts a path to uppercase, keeping the case of the first two path
// elements (group name and disk name)
//
// -> c = pointer to string to convert
// <- --
//----------------------------------------------------------------------------
void upcase(char *c)
{
int i, j=0, k=0;
if(NULL==c) return;
while((k<3)&&(j<(int)strlen(c))) if('\\'==c[j++]) k++;
for(i=j; i<(int)strlen(c); i++) c[i] = toupper(c[i]);
}
//----------------------------------------------------------------------------
// FixDirectoryEntries
//
// Calculates the length of all files in a given directory from the FAT
// entries and updates the corresponding directory entries.
//
// This fixes wrong file sizes.
//
// -> pDisk = pointer to disk
// pDir = pointer to directory
// <- ERR_OK
// ERR_WRITE
//----------------------------------------------------------------------------
int FixDirectoryEntries(DISK *pDisk, ENSONIQDIR *pDir)
{
int i, iResult;
DWORD dwBlock;
LOG("Fixing directory entries\n");
for(i=0; i<39; i++)
{
if(FILE_TYPE_EMPTY==pDir->ucDirectory[i*26+1]) continue; // skip blank entries
if(FILE_TYPE_PARENT_DIRECTORY==pDir->ucDirectory[i*26+1]) continue; // skip link to parent
if(FILE_TYPE_DIRECTORY==pDir->ucDirectory[i*26+1]) continue; // skip subdirectories
// calculate real length according to FAT
pDir->Entry[i].dwLen = 0;
dwBlock = pDir->Entry[i].dwStart;
while(dwBlock>2)
{
pDir->Entry[i].dwLen++;
dwBlock = GetFATEntry(pDisk, dwBlock);
}
pDir->ucDirectory[i*26 + 14] = pDir->Entry[i].dwLen >> 8;
pDir->ucDirectory[i*26 + 15] = pDir->Entry[i].dwLen & 0xFF;
LOG("%s: LEN = 0x%02X 0x%02X START = 0x%02X 0x%02X 0x%02X 0x%02X\n",
pDir->Entry[i].cName,
pDir->ucDirectory[i*26 + 14],
pDir->ucDirectory[i*26 + 15],
pDir->ucDirectory[i*26 + 18],
pDir->ucDirectory[i*26 + 19],
pDir->ucDirectory[i*26 + 20],
pDir->ucDirectory[i*26 + 21]);
iResult = WriteBlocks(pDisk, pDir->dwDirectoryBlock, 2, pDir->ucDirectory);
if(ERR_OK!=iResult)
{
LOG("Error writing Ensoniq file, code=%d\n", iResult);
return ERR_WRITE;
}
iResult = CacheFlush(pDisk);
if(ERR_OK!=iResult)
{
LOG("Error flushing cache, code=%d\n", iResult);
return ERR_WRITE;
}
}
return ERR_OK;
}
//----------------------------------------------------------------------------
// ReadDirectory
//
// Reads the directory starting with dwBlock from pDisk
//
// -> pDisk: pointer to valid disk structure
// dwBlock: starting block
// pDir: ENSONIQDIR structure to receive directory listing
// ucShowWarning: Show warning about bad file sizes
// <- ERR_OK
// ERR_NOT_OPEN
// ERR_OUT_OF_BOUNDS
// ERR_READ
// ERR_MEM
// ERR_NOT_SUPPORTED
// ERR_SEEK
//----------------------------------------------------------------------------
int ReadDirectory(DISK *pDisk, ENSONIQDIR *pDir, unsigned char ucShowWarning)
{
char cWarningMsg[2048] = "Some files in this directory have invalid "
"file sizes according to their directory\nentries. "
"You should consider using ETools to scan and/or repair "
"this disc.\n\n"
"The following files have differing values for directory entry "
"and size\ncalculation from FAT:\n\n";
char cWarning[256];
unsigned char ucWarningFlag = 0;
int iResult, i, j, iIndex;
DWORD dwBlock, dwLen, dwStart, dwContiguous;
LOG("ReadDirectory(block=%d)\n", pDir->dwDirectoryBlock);
// initialize ENSONIQDIR, but keep dwDirectoryBlock
dwBlock = pDir->dwDirectoryBlock;
memset(pDir, 0, sizeof(ENSONIQDIR));
pDir->dwDirectoryBlock = dwBlock;
// read directory blocks from disk
iResult = ReadBlocks(pDisk, dwBlock, 2, pDir->ucDirectory);
if(ERR_OK!=iResult) return iResult;
// loop through all entries
// LOG("\n");
for(i=0; i<39; i++)
{
pDir->Entry[i].ucType = pDir->ucDirectory[i*26+1];
memset(&(pDir->VirtualWaveEntry[i]), 0, sizeof(VIRTUALWAVEFILE));
/*
// debug output
LOG("Directory entry #"); LOG_HEX2(i); LOG(": ");
for(j=0; j<26; j++)
{
LOG_HEX2(pDir->ucDirectory[i*26+j]);
LOG(" ");
}
LOG("\n");
*/
// check type
if(0x00==pDir->ucDirectory[i*26+1]) continue; // skip blank entries
if(0x08==pDir->ucDirectory[i*26+1]) continue; // skip link to parent
// copy name
strncpy(pDir->Entry[i].cName, pDir->ucDirectory+i*26+2, 12);
while(strlen(pDir->Entry[i].cName)<12)
strcat(pDir->Entry[i].cName, " ");
pDir->Entry[i].cName[12] = 0;
strncpy(pDir->Entry[i].cLegalName, pDir->ucDirectory+i*26+2, 12);
MakeLegalName(pDir->Entry[i].cLegalName);
// calculate file properties
pDir->Entry[i].dwLen = (pDir->ucDirectory[i*26 + 14]<<8)
+ pDir->ucDirectory[i*26 + 15];
pDir->Entry[i].dwContiguous = (pDir->ucDirectory[i*26 + 16]<<8)
+ pDir->ucDirectory[i*26 + 17];
if(0==pDir->Entry[i].dwLen)
{
pDir->Entry[i].dwLen = pDir->Entry[i].dwContiguous;
}
pDir->Entry[i].dwStart = (pDir->ucDirectory[i*26 + 18]<<24)
+ (pDir->ucDirectory[i*26 + 19]<<16)
+ (pDir->ucDirectory[i*26 + 20]<<8)
+ pDir->ucDirectory[i*26 + 21];
pDir->Entry[i].ucMultiFileIndex = pDir->ucDirectory[i*26 + 22];
// calculate real length according to FAT
dwLen = 0;
dwBlock = pDir->Entry[i].dwStart;
while(dwBlock>2)
{
dwLen++;
dwBlock = GetFATEntry(pDisk, dwBlock);
}
/* LOG("%s: LEN = 0x%02X 0x%02X START = 0x%02X 0x%02X 0x%02X 0x%02X\n",
pDir->Entry[i].cName,
pDir->ucDirectory[i*26 + 14],
pDir->ucDirectory[i*26 + 15],
pDir->ucDirectory[i*26 + 18],
pDir->ucDirectory[i*26 + 19],
pDir->ucDirectory[i*26 + 20],
pDir->ucDirectory[i*26 + 21]);
*/
if((dwLen!=pDir->Entry[i].dwLen)&&(pDir->Entry[i].ucType!=FILE_TYPE_DIRECTORY))
{
LOG("Warning: for file '%s' length FAT (%d) != length directory entry (%d)\n",
pDir->Entry[i].cName, dwLen, pDir->Entry[i].dwLen);
sprintf(cWarning, "%s: %d blocks (dir) / %d blocks (FAT), %d blocks contiguous\n",
pDir->Entry[i].cName, (int)pDir->Entry[i].dwLen, (int)dwLen,
(int)pDir->Entry[i].dwContiguous);
strcat(cWarningMsg, cWarning);
ucWarningFlag = 1;
}
}
if(ucWarningFlag&&ucShowWarning)
{
strcat(cWarningMsg, "\n\nShould this problem be corrected?");
if(IDYES == MessageBoxA(TC_HWND, cWarningMsg, "EnsoniqFS · Warning",
MB_ICONWARNING | MB_YESNO))
{
if(ERR_OK!=FixDirectoryEntries(pDisk, pDir))
{
MessageBoxA(TC_HWND, "Could not write directory.", "EnsoniqFS · Error",
MB_ICONSTOP);
}
}
}
// loop again through all entries to find ASR audio tracks and
// assign virtual wave files to them
iIndex = 0;
for(i=0; i<39; i++)
{
// skip all non-audio-track-files
if(pDir->Entry[i].ucType != FILE_TYPE_ASR_AUDIOTRACK) continue;
// this is a new audio track
strcpy(pDir->VirtualWaveEntry[iIndex].cName, pDir->Entry[i].cName);
strcpy(pDir->VirtualWaveEntry[iIndex].cLegalName, pDir->Entry[i].cLegalName);
dwLen = pDir->Entry[i].dwLen;
dwStart = pDir->Entry[i].dwStart;
dwContiguous = pDir->Entry[i].dwContiguous;
pDir->VirtualWaveEntry[iIndex].dwLen1 = dwLen;
pDir->VirtualWaveEntry[iIndex].dwStart1 = dwStart;
pDir->VirtualWaveEntry[iIndex].dwContiguous1 = dwContiguous;
pDir->VirtualWaveEntry[iIndex].ucIsStereo = 0;
LOG("audio track '%s', len=%d\n", pDir->VirtualWaveEntry[iIndex].cLegalName, dwLen);
// search for a matching file among the already scanned files
// to form a stereo pair
for(j=iIndex-1; j>=0; j--)
{
// LOG("comparing audio track '%s', len=%d to '%s, len=%d\n",
// pDir->VirtualWaveEntry[iIndex].cLegalName, dwLen,
// pDir->VirtualWaveEntry[j].cLegalName, pDir->VirtualWaveEntry[j].dwLen1);
if(dwLen==pDir->VirtualWaveEntry[j].dwLen1)
{
iIndex++; // store stereo information in next record
pDir->VirtualWaveEntry[iIndex].ucIsStereo = 1;
pDir->VirtualWaveEntry[iIndex].dwLen2 =
pDir->VirtualWaveEntry[j].dwLen1;
pDir->VirtualWaveEntry[iIndex].dwStart2 =
pDir->VirtualWaveEntry[j].dwStart1;
pDir->VirtualWaveEntry[iIndex].dwContiguous2 =
pDir->VirtualWaveEntry[j].dwContiguous1;
pDir->VirtualWaveEntry[iIndex].dwLen1 = dwLen;
pDir->VirtualWaveEntry[iIndex].dwStart1 = dwStart;
pDir->VirtualWaveEntry[iIndex].dwContiguous1 = dwContiguous;
strcpy(pDir->VirtualWaveEntry[iIndex].cName,
pDir->VirtualWaveEntry[j].cName);
strcpy(pDir->VirtualWaveEntry[iIndex].cLegalName,
pDir->VirtualWaveEntry[j].cLegalName);
LOG("Stereo pair detected: name='%s', len1=%d, len2=%d, start1=%d, start2=%d.\n",
pDir->VirtualWaveEntry[iIndex].cLegalName,
pDir->VirtualWaveEntry[iIndex].dwLen1,
pDir->VirtualWaveEntry[iIndex].dwLen2,
pDir->VirtualWaveEntry[iIndex].dwStart1,
pDir->VirtualWaveEntry[iIndex].dwStart2);
break;
}
}
iIndex++;
}
return ERR_OK;
}
//----------------------------------------------------------------------------
// ReadWaveFile
//
// Reads a file using the FAT to a local file in WAV format (44 bytes header)
//
// -> pDisk = pointer to disk to use
// pVirtualWaveFile = pointer to directory entry describing the wave file
// cDestFN = name of file on local disk to receive data of Ensoniq file
// cSourceFN = name of source file (only for progress reporting)
// <- ERR_OK
// ERR_FAT
// ERR_NOT_OPEN
// ERR_OUT_OF_BOUNDS
// ERR_READ
// ERR_MEM
// ERR_NOT_SUPPORTED
// ERR_LOCAL_WRITE
// ERR_ABORTED
// ERR_SEEK
//----------------------------------------------------------------------------
int ReadWaveFile(DISK *pDisk, VIRTUALWAVEFILE *pVirtualWaveFile, char *cDestFN,
char *cSourceFN)
{
unsigned char ucBuf1[512], ucBuf2[512], ucStereoBuf[1024], ucTemp;
int i, j, iResult, iProgress, iLastProgress = -1;
DWORD dwByteRate, dwBlock1, dwBlock2, dwChunkSize, dwSampleRate, dwSize;
FILE *f;
// TODO: Handle multi disk audio tracks (priority: very low, multi disk
// audio tracks are theoretically not possible)
dwSize = pVirtualWaveFile->dwLen1 * 512;
if(pVirtualWaveFile->ucIsStereo) dwSize *= 2;
dwChunkSize = dwSize + 36;
dwSampleRate = 44100;
dwByteRate = dwSampleRate*2; // 16 bits
if(pVirtualWaveFile->ucIsStereo) dwByteRate *= 2;
LOG("ReadWaveFile('%s'->'%s', size=%d bytes)\n", cSourceFN, cDestFN,
dwSize);
// open destination file
f = fopen(cDestFN, "wb");
if(NULL==f)
{
LOG("Error opening destination file for writing.\n");
return ERR_NOT_OPEN;
}
#define BYTE0(x) ((x>> 0)&0xFF)
#define BYTE1(x) ((x>> 8)&0xFF)
#define BYTE2(x) ((x>>16)&0xFF)
#define BYTE3(x) ((x>>24)&0xFF)
// write WAV header to file
LOG("Writing WAV header... ");
memset(ucBuf1, 0, 512);
ucBuf1[ 0] = 'R';
ucBuf1[ 1] = 'I';
ucBuf1[ 2] = 'F';
ucBuf1[ 3] = 'F';
ucBuf1[ 4] = BYTE0(dwChunkSize);
ucBuf1[ 5] = BYTE1(dwChunkSize);
ucBuf1[ 6] = BYTE2(dwChunkSize);
ucBuf1[ 7] = BYTE3(dwChunkSize);
ucBuf1[ 8] = 'W';
ucBuf1[ 9] = 'A';
ucBuf1[10] = 'V';
ucBuf1[11] = 'E';
ucBuf1[12] = 'f';
ucBuf1[13] = 'm';
ucBuf1[14] = 't';
ucBuf1[15] = ' ';
ucBuf1[16] = 16; // sub chunk size (16)
ucBuf1[17] = 0;
ucBuf1[18] = 0;
ucBuf1[19] = 0;
ucBuf1[20] = 0x01; // format = PCM (0x0001)
ucBuf1[21] = 0x00;
ucBuf1[22] = (pVirtualWaveFile->ucIsStereo) ? 0x02 : 0x01;
ucBuf1[23] = 0x00;
ucBuf1[24] = BYTE0(dwSampleRate);
ucBuf1[25] = BYTE1(dwSampleRate);
ucBuf1[26] = BYTE2(dwSampleRate);
ucBuf1[27] = BYTE3(dwSampleRate);
ucBuf1[28] = BYTE0(dwByteRate); // byte rate
ucBuf1[29] = BYTE1(dwByteRate);
ucBuf1[30] = BYTE2(dwByteRate);
ucBuf1[31] = BYTE3(dwByteRate);
ucBuf1[32] = (pVirtualWaveFile->ucIsStereo) ? 0x04 : 0x02; // block align
ucBuf1[33] = 0x00;
ucBuf1[34] = 16; // 16 bits per sample
ucBuf1[35] = 0;
ucBuf1[36] = 'd';
ucBuf1[37] = 'a';
ucBuf1[38] = 't';
ucBuf1[39] = 'a';
ucBuf1[40] = BYTE0(dwSize);
ucBuf1[41] = BYTE1(dwSize);
ucBuf1[42] = BYTE2(dwSize);
ucBuf1[43] = BYTE3(dwSize);
if(44!=fwrite(ucBuf1, 1, 44, f))
{
LOG("Error writing to destination file.\n");
fclose(f);
return ERR_LOCAL_WRITE;
}
LOG("Extracting file... ");
// set starting block
dwBlock1 = pVirtualWaveFile->dwStart1;
dwBlock2 = pVirtualWaveFile->dwStart2;
// loop through all blocks
for(i=0; i<(int)pVirtualWaveFile->dwLen1; i++)
{
// read next block from disk image
iResult = ReadBlock(pDisk, dwBlock1, ucBuf1);
if(ERR_OK!=iResult)
{
LOG("Error reading block, code=%d.\n", iResult);
fclose(f);
return iResult;
}
if(pVirtualWaveFile->ucIsStereo)
{
iResult = ReadBlock(pDisk, dwBlock2, ucBuf2);
if(ERR_OK!=iResult)
{
LOG("Error reading block, code=%d.\n", iResult);
fclose(f);
return iResult;
}
// swap bytes and multiplex two channels
for(j=0; j<256; j++)
{
ucStereoBuf[j*4+0] = ucBuf1[j*2+1];
ucStereoBuf[j*4+1] = ucBuf1[j*2+0];
ucStereoBuf[j*4+2] = ucBuf2[j*2+1];
ucStereoBuf[j*4+3] = ucBuf2[j*2+0];
}
// write next block to file
if(1024!=fwrite(ucStereoBuf, 1, 1024, f))
{
LOG("Error writing to destination file.\n");
fclose(f);
return ERR_LOCAL_WRITE;
}
// get next FAT entry
dwBlock2 = GetFATEntry(pDisk, dwBlock2);
}
else
{
// swap bytes, only one channel
for(j=0; j<256; j++)
{
ucTemp = ucBuf1[j*2];
ucBuf1[j*2] = ucBuf1[j*2+1];
ucBuf1[j*2+1] = ucTemp;
}
// write next block to file
if(512!=fwrite(ucBuf1, 1, 512, f))
{
LOG("Error writing to destination file.\n");
fclose(f);
return ERR_LOCAL_WRITE;
}
}
// get next FAT entry
dwBlock1 = GetFATEntry(pDisk, dwBlock1);
// either end of file reached or error getting FAT entry occured
if((dwBlock1<3)||((pVirtualWaveFile->ucIsStereo)&&(dwBlock2<3)))
{
LOG("Warning: EOF in FAT occured before file length was reached.\n");
break;
}
// notify TotalCmd of progress
iProgress = i*100/pVirtualWaveFile->dwLen1;
if((iProgress-iLastProgress)>5)
{
if(1==g_pProgressProc(g_iPluginNr, cSourceFN, cDestFN, iProgress))
{
fclose(f);
return ERR_ABORTED;
}
iLastProgress = iProgress;
}
}
LOG("OK.\n");
fclose(f);
// notify TotalCmd of progress
if(1==g_pProgressProc(g_iPluginNr, cSourceFN, cDestFN, 100))
return ERR_ABORTED;
return ERR_OK;
}
DISK *SearchDisk(char *cMsDosName)
{
DISK *pDisk = g_pDiskListRoot;
while(pDisk)
{
if(0==strcmp(cMsDosName, pDisk->cMsDosName))
{
return pDisk;
}
pDisk = pDisk->pNext;
}
return 0;
}
int SelectNextDisk(DISK **pDisk, DWORD *dwBlock, ENSONIQDIRENTRY *pDirEntry)
{
char cMessage[512], *cMsDosName;
DISK *pNewDisk;
ENSONIQDIRENTRY *pe;
int i, iResult;
// loop until a valid next disk is selected or user aborts
// -> user gets chance to switch disks or disk images multiple
// times if choice was wrong
while(1)
{
// ask user for new disk
pNewDisk = 0;
iResult = CreateChooseDiskDialogModal(TC_HWND, &pNewDisk, *pDisk);
if((iResult==IDCANCEL)||(pNewDisk==0))
{
return ERR_ABORTED;
}
// get the MSDOS path of the chosen disk
cMsDosName = malloc(strlen(pNewDisk->cMsDosName));
strcpy(cMsDosName, pNewDisk->cMsDosName);
// reload disk list, just in case user has exchanged some
// removable media
FreeDiskList(0, g_pDiskListRoot);
g_pDiskListRoot = ScanDevices(0);
if(0==g_pDiskListRoot)
{
free(cMsDosName);
if(IDCANCEL==MessageBoxA(TC_HWND,
"There was an error during device scan. "
"Try again?", "EnsoniqFS · Warning",
MB_ICONWARNING | MB_RETRYCANCEL))
{
return ERR_ABORTED;
}
continue; // retry
}
// find the disk from the remembered name
pNewDisk = SearchDisk(cMsDosName);
free(cMsDosName);
if(0==pNewDisk)
{
if(IDCANCEL==MessageBoxA(TC_HWND,
"The selected disk could not be found. "
"Try again?", "EnsoniqFS · Warning",
MB_ICONWARNING | MB_RETRYCANCEL))
{
return ERR_ABORTED;
}
continue; // retry
}
// read root directory
ENSONIQDIR e;
e.dwDirectoryBlock = 3;
if(ERR_OK!=ReadDirectory(pNewDisk, &e, 0))
{
if(IDCANCEL==MessageBoxA(TC_HWND,
"Could not read the selected disk. "
"Retry?", "EnsoniqFS · Warning",
MB_ICONWARNING | MB_RETRYCANCEL))
{
return ERR_ABORTED;
}
continue; // retry
}
// search the new directory for the file in question
for(i=0; i<39; i++)
{
pe = &e.Entry[i];
// skip empty lines and directory pointers
if(pe->ucType==FILE_TYPE_EMPTY) continue;
if(pe->ucType==FILE_TYPE_DIRECTORY) continue;
if(pe->ucType==FILE_TYPE_PARENT_DIRECTORY) continue;
// continue if type or name differs
if(pe->ucType!=pDirEntry->ucType) continue;
if(0!=strncmp(pe->cName, pDirEntry->cName, 12)) continue;
// if we reach this point, all checks have been made
// -> this is our file, leave the loop
break;
}
// Did we reach the end of the directory without finding our file?
if(i>=39)
{
if(IDCANCEL==MessageBoxA(TC_HWND,
"Could not find the multi disk file on the selected disk. "
"Retry?", "EnsoniqFS · Warning",
MB_ICONWARNING | MB_RETRYCANCEL))
{
return ERR_ABORTED;
}
continue; // retry
}
// check multi disk index
if(pe->ucMultiFileIndex != pDirEntry->ucMultiFileIndex+1)
{
sprintf(cMessage,
"The selected disk contains the requested file, but this\n"
"is part %d instead of %d. Retry?",
pe->ucMultiFileIndex, pDirEntry->ucMultiFileIndex+1);
if(IDCANCEL==MessageBoxA(TC_HWND,
cMessage, "EnsoniqFS · Warning",
MB_ICONWARNING | MB_RETRYCANCEL))
{
return ERR_ABORTED;
}
continue; // retry
}
// Switch to next part
memcpy(pDirEntry, pe, sizeof(ENSONIQDIRENTRY));
*pDisk = pNewDisk;
*dwBlock = pe->dwStart;
break;
}
return ERR_OK;
}
int getNameFromEnsoniqFileItem( unsigned char* pData, unsigned char* cDestName )
{
if (pData == NULL) return ERR_ABORTED;
if (cDestName == NULL) return ERR_ABORTED;
// name of instruments starts at 0x000a
unsigned int nameOffset = 0x000a;
unsigned char charCounter;
for( charCounter = 0; charCounter < 12; charCounter++)
{
cDestName[charCounter] = pData[ nameOffset + charCounter*2 ];
}
cDestName[12] = 0;
return ERR_OK;
}
int setNameToEnsoniqFileItem( unsigned char* pData, const unsigned char* cNewName )
{
if (pData == NULL) return ERR_ABORTED;
if (cNewName == NULL) return ERR_ABORTED;
// name of instruments starts at 0x000a
unsigned int nameOffset = 0x000a;
unsigned char charCounter;
for( charCounter = 0; charCounter < 12; charCounter++)
{
pData[ nameOffset + charCounter*2 ] = cNewName[charCounter];
}
return ERR_OK;
}
int RenameEnsoniqItem( DISK *pDisk, ENSONIQDIRENTRY *pDirEntry, const char* cOldName, const char *cNewName )
{
// check pointers
if(NULL==pDisk) return ERR_ABORTED;
if(NULL==pDirEntry) return ERR_ABORTED;
if(NULL==cOldName) return ERR_ABORTED;
if(NULL==cNewName) return ERR_ABORTED;
unsigned char ucBuf[512];
unsigned char ucCurrentName[13];
int iResult;
DWORD dwBlock;
// set starting block
dwBlock = pDirEntry->dwStart;
// read first block (instrument header)
iResult = ReadBlock(pDisk, dwBlock, ucBuf);
if(ERR_OK!=iResult)
{
LOG("SetNameOfEnsoniqFile: Error reading block, code=%d.\n", iResult);
return iResult;
}
// TODO : this works only for instruments
iResult = getNameFromEnsoniqFileItem( ucBuf, ucCurrentName );
if(ERR_OK!=iResult)
{
LOG("SetNameOfEnsoniqFile: Error retrieving current name, code=%d.\n", iResult);
return iResult;
}
LOG("Current name: >%s<\n", ucCurrentName);
if (0 != strcmp(ucCurrentName, cOldName))
{
LOG("SetNameOfEnsoniqFile: Mismatch between current name in file system and name in item.\n");
return ERR_ABORTED;
}
// TODO : this works only for instruments
iResult = setNameToEnsoniqFileItem( ucBuf, cNewName );
if(ERR_OK!=iResult)
{
LOG("SetNameOfEnsoniqFile: Error setting new name, code=%d.\n", iResult);
return iResult;
}
iResult = WriteBlocks(pDisk, dwBlock, 1, ucBuf);
if(ERR_OK!=iResult)
{
LOG("SetNameOfEnsoniqFile: Error writing Ensoniq file, code=%d.\n", iResult);
return ERR_WRITE;
}
return ERR_OK;
}
//----------------------------------------------------------------------------
// ReadEnsoniqFile
//
// Reads a file using the FAT to a local file in EFE format (512 bytes header)
//
// -> pDisk = pointer to disk to use
// pDirEntry = pointer to directory entry describing the Ensoniq file
// cDestFN = name of file on local disk to receive data of Ensoniq file
// cSourceFN = name of source file (only for progress reporting)
// <- ERR_OK
// ERR_FAT
// ERR_NOT_OPEN
// ERR_OUT_OF_BOUNDS
// ERR_READ
// ERR_MEM
// ERR_NOT_SUPPORTED
// ERR_LOCAL_WRITE
// ERR_ABORTED
// ERR_SEEK
//----------------------------------------------------------------------------
int ReadEnsoniqFile(DISK *pDisk, ENSONIQDIRENTRY *pDirEntry, char *cDestFN,
char *cSourceFN)
{
unsigned char ucBuf[512], ucCopyMultidisk = 0;
char cFiletype[13];
int i, iResult, iProgress, iLastProgress = -1;
DWORD dwBlock, dwSize, dwBlockCounter = 0;
FILE *f;
LOG("Reading Ensoniq file (Start=%d, size=%d blocks, type=%d, "
"destination='%s'\n", pDirEntry->dwStart, pDirEntry->dwLen,
pDirEntry->ucType, cDestFN);
// check pointers
if(NULL==pDisk) return ERR_NOT_OPEN;
if(NULL==pDirEntry) return ERR_NOT_OPEN;
if(NULL==cDestFN) return ERR_LOCAL_WRITE;
// apply special treatment if this is a multi disk file
if(pDirEntry->ucMultiFileIndex)
{
// ask if multi disk copy is required
if(1==pDirEntry->ucMultiFileIndex)
{
i = MessageBoxA(TC_HWND, "This file is the first part of a "
"multi-disk file. To merge all parts\n"
"into one file make sure you have\n"
" (a) all necessary removable disks ready\n"
" or\n"
" (b) all necessary fixed disks or image files mounted.\n\n"
"Do you want to merge all files (Yes), copy only this single\n"
"file (No) or cancel the process?",
"EnsoniqFS · Warning",
MB_ICONWARNING | MB_YESNOCANCEL);
if(i==IDCANCEL)
{
return ERR_ABORTED;
}
if(i==IDYES)
{
ucCopyMultidisk = 1;
}
}
else
{
// warn user that this is some middle part of a multi disk file
if(IDNO == MessageBoxA(TC_HWND, "This file is the middle or end part of a "
"multi-disk file. To join all parts\n"
"to one file you have to start with the first part.\n\n"
"Do you want to copy this part only?",
"EnsoniqFS · Warning",
MB_ICONWARNING | MB_YESNO))
{
return ERR_ABORTED;
}
}
}
// notify TotalCmd of progress
if(1==g_pProgressProc(g_iPluginNr, cSourceFN, cDestFN, 0))
return ERR_ABORTED;
// open destination file
f = fopen(cDestFN, "wb");
if(NULL==f)
{
LOG("Error opening destination file for writing.\n");
return ERR_NOT_OPEN;
}
// use length from directory entry as size; this can change for multi disk
// files once the first block has been read and the real file size is known
dwSize = pDirEntry->dwLen;
// set starting block
dwBlock = pDirEntry->dwStart;
if(ucCopyMultidisk)
{
// read first block (instrument header)
iResult = ReadBlock(pDisk, dwBlock, ucBuf);
if(ERR_OK!=iResult)
{
LOG("Error reading block, code=%d.\n", iResult);
fclose(f);
return iResult;
}
// get real size from instrument header
dwSize = ((DWORD)ucBuf[0x28] << 8) + (DWORD)ucBuf[0x29];
LOG("Reading multi disk file, real size=%d blocks.\n", dwSize);
}
// write EFE header to file
LOG("Writing header... ");
memset(ucBuf, 0, 512);
sprintf(ucBuf, "\r\nEps File: %s ", pDirEntry->cName);
GetShortEnsoniqFiletype(pDirEntry->ucType, cFiletype);
strcat(ucBuf, cFiletype);
strcat(ucBuf, "\r\n");
ucBuf[0x31] = 0x1A;
ucBuf[0x32] = pDirEntry->ucType;
ucBuf[0x34] = (dwSize>>8)&0xFF;
ucBuf[0x35] = (dwSize)&0xFF;
ucBuf[0x36] = (pDirEntry->dwContiguous>>8)&0xFF;
ucBuf[0x37] = (pDirEntry->dwContiguous)&0xFF;
ucBuf[0x38] = (pDirEntry->dwStart>>8)&0xFF;
ucBuf[0x39] = (pDirEntry->dwStart)&0xFF;
ucBuf[0x3A] = pDirEntry->ucMultiFileIndex;
// reset the multi disk field if we are about to copy all parts
// and merge them into one file which isn't multi disk anymore afterwards
if(ucCopyMultidisk) ucBuf[0x3A] = 0;
if(512!=fwrite(ucBuf, 1, 512, f))
{
LOG("Error writing to destination file.\n");
fclose(f);
return ERR_LOCAL_WRITE;
}
LOG("Extracting file... ");
// loop through all blocks
i = 0;
while(dwBlockCounter<dwSize)
{
// read next block from disk image
iResult = ReadBlock(pDisk, dwBlock, ucBuf);
if(ERR_OK!=iResult)
{
LOG("Error reading block, code=%d.\n", iResult);
fclose(f);
return iResult;
}
// write next block to file
if(512!=fwrite(ucBuf, 1, 512, f))
{
LOG("Error writing to destination file.\n");
fclose(f);
return ERR_LOCAL_WRITE;
}
// get next FAT entry
dwBlock = GetFATEntry(pDisk, dwBlock);
// either end of file reached or error getting FAT entry occured
if(dwBlock<3)
{
LOG("FAT entry=%d, i=%d, Len=%d - stopping.\n", dwBlock, i,
pDirEntry->dwLen);
i = pDirEntry->dwLen; // stop here even if file is too short
}