-
Notifications
You must be signed in to change notification settings - Fork 18
/
vgm_tag.c
1374 lines (1248 loc) · 35.8 KB
/
vgm_tag.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
// vgm_tag.c - VGM Tagger
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // for toupper()
#include <wctype.h>
#include <locale.h>
#include <wchar.h>
#include <limits.h> // for MB_LEN_MAX
#include <zlib.h>
#include "stdtype.h"
#include "stdbool.h"
#include "VGMFile.h"
#include "common.h"
typedef struct system_name_shorts
{
char* Abbr;
char* NameE;
char* NameJ;
} SYSTEM_SHORT;
typedef union filePtr
{
gzFile gz;
FILE* f;
} FGZ_PTR;
int main(int argc, char* argv[]);
static bool OpenVGMFile(const char* FileName, bool* Compressed);
static wchar_t* ReadWStrFromFile(gzFile hFile, UINT32* FilePos, const UINT32 EOFPos);
static UINT8 ConvertUnicode2UTF8(char* Buffer, const UINT16 UnicodeChr);
static bool WriteVGMFile(const char* FileName, const bool Compress);
static UINT32 WriteWStrToFile(const FGZ_PTR hFile, const wchar_t* WString);
static void Copy2TagStr(wchar_t** TagStr, const char* DataStr);
static void CopySys2TagStr(wchar_t** TagStr, const SYSTEM_SHORT* DataStr, bool Mode,
const char* CmdStr);
static bool CompareSystemNames(const char* StrA, const char* StrB);
static UINT16 GetSystemString(const char* SystemName);
static UINT8 TagVGM(const int ArgCount, char* ArgList[]);
static bool RemoveEqualTag(wchar_t** TagE, wchar_t** TagJ, UINT8 Mode);
static bool RemoveUnknown(wchar_t** Tag);
static void ShowTag(const UINT8 UnicodeMode);
char* ConvertWStr2ASCII_NCR(const wchar_t* WideStr);
char* ConvertWStr2UTF8(const wchar_t* WideStr);
static wchar_t* wcscap(wchar_t* string);
#define SYSMODE_ENG false
#define SYSMODE_JAP true
const SYSTEM_SHORT SYSTEM_NAMES[] =
// short long (english) long (japanese)
{ {"SMS", "Sega Master System", "セガマスターシステム"},
{"SGG", "Sega Game Gear", "セガゲームギア"},
{"SMSGG", "Sega Master System / Game Gear", "セガマスターシステム / "
"ゲームギア"},
{"SMD", "Sega Mega Drive / Genesis", "セガメガドライブ"},
{"SG1k", "Sega Game 1000", "エスジー・セン"},
{"SC3k", "Sega Computer 3000", "セガコンピュータ 3000"},
{"SS*", "Sega System *", "セガシステム*"}, // for Sega System 1/2/16/18/24/32/C/C-2/E
// {"CPS?", "Capcom Play System ?", ""}, // old name
{"CPS", "CP System", "CP システム"},
{"CPS2", "CP System II", "CP システム II"},
{"CPS3", "CP System III", "CP システム III"},
{"Ccv", "Colecovision", "コレコビジョン"},
{"BMM*", "BBC Micro Model *", ""},
{"BM128", "BBC Master 128", ""},
{"Arc", "Arcade Machine", "アーケードマシーン"},
{"NGP", "Neo Geo Pocket", "ネオジオポケット"},
{"NGPC", "Neo Geo Pocket Color", "ネオジオポケットカラー"},
{"SCD", "Sega MegaCD / SegaCD", "メガCD"},
{"32X", "Sega 32X", "スーパー32X"},
{"SCD32", "Sega Mega-CD 32X / Sega CD 32X", "メガCD 32X"},
{"Nmc*", "Namco System *", "ナムコシステム*"},
{"SX", "Sega X", "セガ X"},
{"SY", "Sega Y", "セガ Y"},
{"SGX", "System GX", "システムGX"},
{"AS?", "Atari System ?", "アタリシステム ?"},
{"BS", "Bubble System", "バブルシステム"},
{"IM*", "Irem M*", "アイレム M*"},
{"TW16", "Twin 16", "ツイン16"},
{"NG", "Neo Geo", "ネオジオ"},
{"NG*", "Neo Geo *", "ネオジオ*"},
{"NES", "Nintendo Entertainment System", "ファミリーコンピュータ"},
{"FDS", "Famicom Disk System", "ファミリーコンピュータ "
"ディスクシステム"},
{"NESFDS", "Nintendo Entertainment System / Famicom Disk System",
"ファミリーコンピュータ / "
"ファミリーコンピュータ "
"ディスクシステム"},
{"GB", "Game Boy", "ゲームボーイ"},
{"GBC", "Game Boy Color", "ゲームボーイカラー"},
{"GBGBC", "Game Boy / Game Boy Color", "ゲームボーイ / "
"ゲームボーイカラー"},
{"GBA", "Game Boy Advance", "ゲームボーイ アドバンス"},
{"TG16", "TurboGrafx-16", "PCエンジン"},
{"TGCD", "TurboGrafx-CD", "PCエンジン CD-ROM2"},
{"Tp?", "Toaplan ?", "東亜プラン ?"},
{"VB", "Virtual Boy", "バーチャルボーイ"},
{NULL, NULL, NULL}};
VGM_HEADER VGMHead;
GD3_TAG VGMTag;
UINT32 VGMDataLen;
UINT8* VGMData;
bool FileCompression; // used to select fwrite or gzwite
int main(int argc, char* argv[])
{
int CmdCnt;
int CurArg;
// if SourceFile is NOT compressed, FileCompr = false -> DestFile will be uncompressed
// if SourceFile IS compressed, FileCompr = true -> DestFile will also be compressed
bool FileCompr;
int ErrVal;
UINT8 RetVal;
setlocale(LC_ALL, ""); // enable UTF-8 support on Linux
printf("VGM Tagger\n----------\n\n");
ErrVal = 0;
if (argc <= 0x01)
{
printf("Usage: vgm_tag [-command1] [-command2] file1.vgm file2.vgz ...\n");
printf("Use argument -help for command list.\n");
goto EndProgram;
}
else if (! stricmp(argv[1], "-Help"))
{
printf("Help\n----\n");
printf("Usage: vgm_tag [-command1] [-command2] file1.vgm file2.vgz\n");
printf("Command List\n------------\n");
printf("General Commands:\n");
printf("(no command) like -ShowTag\n");
printf("\t-Help Show this help\n");
printf("\t-SysList Show the System List\n");
printf("\t-RemoveTag Remove the GD3 tag (following commands are ignored)\n");
printf("\t-ClearTag Clear the GD3 tag\n");
printf("\t-ShowTag Shows the GD3 (HTML NCRs are used to display Unicode-Chars\n");
printf("\t-ShowTagU like above, but tries to print real Unicode-Chars\n");
printf("\t-ShowTag8 like above, but UTF-8 is used to print Unicode-Chars\n");
printf("\t-TitleCase applies title case to the EN title tag and puts\n");
printf("\t the original case in the JP title tag\n");
printf("\n");
printf("Tagging Commands:\n");
printf("Command format: -command:value or -command:\"value with spaces\"\n");
printf("\t-Title Track Title\n");
printf("\t-TitleJ Track Title (Japanese)\n");
printf("\t-Author Track Composer/Author\n");
printf("\t-AuthorJ Track Composer/Author (Japanese)\n");
printf("\t-Game Game Name\n");
printf("\t-GameJ Game Name (Japanese)\n");
printf("\t-System Game System*\n");
printf("\t-SystemE Game System (English)\n");
printf("\t-SystemJ Game System (Japanese)\n");
printf("\t-Year Release Date\n");
printf("\t-Creator VGM Creator\n");
printf("\t-Notes Notes and Comments (replace)\n");
printf("\t-NotesB Notes and Comments (insert at beginning)\n");
printf("\t-NotesE Notes and Comments (append to end)\n");
printf("\t-NotesStripAt Notes tag: strip parameter + rest\n");
printf("\n");
printf("*If the system's short name is given,");
printf("the Japanese system name is filled too.\n");
printf(" Otherwise the English system name is set and the Japanese one is cleared.\n");
printf("Command names are case insensitive.\n\n");
goto EndProgram;
}
else if (! stricmp(argv[1], "-SysList"))
{
printf("System List\n-----------\n");
CurArg = 0x00;
while(SYSTEM_NAMES[CurArg].Abbr != NULL)
{
printf("%s\t%s\n", SYSTEM_NAMES[CurArg].Abbr, SYSTEM_NAMES[CurArg].NameE);
CurArg ++;
}
goto EndProgram;
}
for (CmdCnt = 0x01; CmdCnt < argc; CmdCnt ++)
{
if (*argv[CmdCnt] != '-')
break; // skip all commands
}
if (CmdCnt >= argc)
{
printf("Error: No files specified!\n");
goto EndProgram;
}
for (CurArg = CmdCnt; CurArg < argc; CurArg ++)
{
printf("File: %s ...\n", argv[CurArg]);
if (! OpenVGMFile(argv[CurArg], &FileCompr))
{
printf("Error opening file %s!\n", argv[CurArg]);
printf("\n");
ErrVal |= 1; // There was at least 1 opening-error.
continue;
}
RetVal = TagVGM(CmdCnt - 0x01, argv + 0x01);
if (RetVal == 0x10)
{
// Showed Tag - no write neccessary
}
else if (RetVal)
{
if (RetVal == 0x80)
{
ErrVal |= 8;
goto EndProgram; // Argument Error
}
ErrVal |= 4; // At least 1 file wasn't tagged.
}
else //if (! RetVal)
{
if (! WriteVGMFile(argv[CurArg], FileCompr))
{
printf("Error opening file %s!\n", argv[CurArg]);
ErrVal |= 2; // There was at least 1 writing-error.
}
}
printf("\n");
}
EndProgram:
DblClickWait(argv[0]);
return ErrVal;
}
static bool OpenVGMFile(const char* FileName, bool* Compressed)
{
gzFile hFile;
UINT32 fccHeader;
UINT32 CurPos;
UINT32 TempLng;
hFile = gzopen(FileName, "rb");
if (hFile == NULL)
return false;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &fccHeader, 0x04);
if (fccHeader != FCC_VGM)
goto OpenErr;
*Compressed = ! gzdirect(hFile);
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &VGMHead, 0x40); // I don't need more data
ZLIB_SEEKBUG_CHECK(VGMHead);
// relative -> absolute addresses
VGMHead.lngEOFOffset += 0x00000004;
if (VGMHead.lngGD3Offset)
VGMHead.lngGD3Offset += 0x00000014;
VGMDataLen = VGMHead.lngEOFOffset;
// Read GD3 Tag
if (VGMHead.lngGD3Offset)
{
gzseek(hFile, VGMHead.lngGD3Offset, SEEK_SET);
gzread(hFile, &fccHeader, 0x04);
if (fccHeader != FCC_GD3)
VGMHead.lngGD3Offset = 0x00000000;
//goto OpenErr;
}
if (VGMHead.lngGD3Offset)
{
VGMDataLen = VGMHead.lngGD3Offset; // That's the actual VGM Data without Tag
CurPos = VGMHead.lngGD3Offset;
gzseek(hFile, CurPos, SEEK_SET);
gzread(hFile, &VGMTag, 0x0C);
CurPos += 0x0C;
TempLng = CurPos + VGMTag.lngTagLength;
VGMTag.strTrackNameE = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strTrackNameJ = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strGameNameE = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strGameNameJ = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strSystemNameE = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strSystemNameJ = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strAuthorNameE = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strAuthorNameJ = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strReleaseDate = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strCreator = ReadWStrFromFile(hFile, &CurPos, TempLng);
VGMTag.strNotes = ReadWStrFromFile(hFile, &CurPos, TempLng);
}
// Read Data
VGMData = (UINT8*)malloc(VGMDataLen);
if (VGMData == NULL)
goto OpenErr;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, VGMData, VGMDataLen);
gzclose(hFile);
return true;
OpenErr:
gzclose(hFile);
return false;
}
static wchar_t* ReadWStrFromFile(gzFile hFile, UINT32* FilePos, const UINT32 EOFPos)
{
UINT32 CurPos;
wchar_t* TextStr;
wchar_t* TempStr;
UINT32 StrLen;
UINT16 UnicodeChr;
// Unicode 2-Byte -> 4-Byte conversion is not neccessary,
// but it's easier to handle wchar_t than unsigned short
// (note: wchar_t is 16-bit on Windows, but 32-bit on Linux)
CurPos = *FilePos;
TextStr = (wchar_t*)malloc((EOFPos - CurPos) / 0x02 * sizeof(wchar_t));
if (TextStr == NULL)
return NULL;
gzseek(hFile, CurPos, SEEK_SET);
TempStr = TextStr;
StrLen = 0x00;
do
{
gzread(hFile, &UnicodeChr, 0x02);
*TempStr = (wchar_t)UnicodeChr;
TempStr ++;
CurPos += 0x02;
StrLen ++;
if (CurPos >= EOFPos)
break;
} while(*(TempStr - 1));
TextStr = (wchar_t*)realloc(TextStr, StrLen * sizeof(wchar_t));
*FilePos = CurPos;
return TextStr;
}
static UINT8 ConvertUnicode2UTF8(char* Buffer, const UINT16 UnicodeChr)
{
// Convert Unicode to UTF-8
// return legnth of written data
if (UnicodeChr & 0xF800)
{
// 0800 - FFFF
Buffer[0x00] = 0xE0 | ((UnicodeChr >> 12) & 0x0F);
Buffer[0x01] = 0x80 | ((UnicodeChr >> 6) & 0x3F);
Buffer[0x02] = 0x80 | ((UnicodeChr >> 0) & 0x3F);
return 0x03;
}
else if (UnicodeChr & 0x0780)
{
// 0080 - 07FF
Buffer[0x00] = 0xC0 | ((UnicodeChr >> 6) & 0x1F);
Buffer[0x01] = 0x80 | ((UnicodeChr >> 0) & 0x3F);
return 0x02;
}
else //if (UnicodeChr & 0x007F)
{
// 0000 - 007F
Buffer[0x00] = 0x00 | (UnicodeChr & 0x7F);
return 0x01;
}
}
static bool WriteVGMFile(const char* FileName, const bool Compress)
{
FGZ_PTR hFile;
FileCompression = Compress;
if (! FileCompression)
hFile.f = fopen(FileName, "wb");
else
hFile.gz = gzopen(FileName, "wb9");
if (hFile.f == NULL)
return false;
// Write VGM Data (excluding GD3 Tag)
if (! FileCompression)
{
fseek(hFile.f, 0x00, SEEK_SET);
fwrite(VGMData, 0x01, VGMDataLen, hFile.f);
}
else
{
gzseek(hFile.gz, 0x00, SEEK_SET);
gzwrite(hFile.gz, VGMData, VGMDataLen);
}
// Write GD3 Tag
if (VGMHead.lngGD3Offset)
{
if (! FileCompression)
{
fseek(hFile.f, VGMHead.lngGD3Offset, SEEK_SET);
fwrite(&VGMTag.fccGD3, 0x04, 0x01, hFile.f);
fwrite(&VGMTag.lngVersion, 0x04, 0x01, hFile.f);
fwrite(&VGMTag.lngTagLength, 0x04, 0x01, hFile.f);
}
else
{
gzseek(hFile.gz, VGMHead.lngGD3Offset, SEEK_SET);
gzwrite(hFile.gz, &VGMTag.fccGD3, 0x04);
gzwrite(hFile.gz, &VGMTag.lngVersion, 0x04);
gzwrite(hFile.gz, &VGMTag.lngTagLength, 0x04);
}
WriteWStrToFile(hFile, VGMTag.strTrackNameE);
WriteWStrToFile(hFile, VGMTag.strTrackNameJ);
WriteWStrToFile(hFile, VGMTag.strGameNameE);
WriteWStrToFile(hFile, VGMTag.strGameNameJ);
WriteWStrToFile(hFile, VGMTag.strSystemNameE);
WriteWStrToFile(hFile, VGMTag.strSystemNameJ);
WriteWStrToFile(hFile, VGMTag.strAuthorNameE);
WriteWStrToFile(hFile, VGMTag.strAuthorNameJ);
WriteWStrToFile(hFile, VGMTag.strReleaseDate);
WriteWStrToFile(hFile, VGMTag.strCreator);
WriteWStrToFile(hFile, VGMTag.strNotes);
}
if (! FileCompression)
fclose(hFile.f);
else
gzclose(hFile.gz);
printf("Tag written.\n");
return true;
}
static UINT32 WriteWStrToFile(const FGZ_PTR hFile, const wchar_t* WString)
{
const wchar_t* TextStr;
UINT32 WrittenChars;
UINT16 UnicodeChr;
TextStr = WString;
WrittenChars = 0x00;
while(*TextStr)
{
UnicodeChr = (wchar_t)(*TextStr);
if (! FileCompression)
fwrite(&UnicodeChr, 0x02, 0x01, hFile.f);
else
gzwrite(hFile.gz, &UnicodeChr, 0x02);
TextStr ++;
WrittenChars ++;
}
// Write Null-Terminator
UnicodeChr = 0x0000;
if (! FileCompression)
fwrite(&UnicodeChr, 0x02, 0x01, hFile.f);
else
gzwrite(hFile.gz, &UnicodeChr, 0x02);
return WrittenChars;
}
static void Copy2TagStr(wchar_t** TagStr, const char* DataStr)
{
int ChrLen;
size_t DataLen;
const char* SrcStr;
wchar_t* DstStr;
const char* ChrStr;
UINT16 UnicodeChr;
char NCRStr[0x10];
if (DataStr != NULL)
DataLen = strlen(DataStr);
else
DataLen = 0x00;
*TagStr = (wchar_t*)realloc(*TagStr, (DataLen + 0x01) * sizeof(wchar_t));
SrcStr = DataStr;
DstStr = *TagStr;
DataLen = 0x00;
while(SrcStr && *SrcStr)
{
if (*SrcStr == '\\')
{
SrcStr ++;
// Handle C-style Escape-Sequences (only \n is useful)
switch(*SrcStr)
{
case 'n':
*DstStr = L'\n';
SrcStr ++;
DstStr ++;
DataLen ++;
continue;
case '\\':
*DstStr = L'\\';
SrcStr ++;
DstStr ++;
DataLen ++;
continue;
}
}
else if (*SrcStr == '&')
{
// HTML NCR
ChrStr = SrcStr;
ChrStr ++;
if (*ChrStr == '#')
{
ChrStr ++;
// Format: Ӓ -> Unicode Char 01234
// ꯍ -> Unicode Char 0xABCD
UnicodeChr = 0x00;
while(*ChrStr && *ChrStr != ';')
{
if (UnicodeChr < 0x0F)
{
NCRStr[UnicodeChr] = *ChrStr;
UnicodeChr ++;
}
ChrStr ++;
}
NCRStr[UnicodeChr] = 0x00;
ChrStr ++;
if (tolower(*NCRStr) == 'x')
UnicodeChr = (UINT16)strtoul(NCRStr + 0x01, NULL, 0x10);
else
UnicodeChr = (UINT16)strtoul(NCRStr, NULL, 10);
*DstStr = (wchar_t)UnicodeChr;
SrcStr = ChrStr;
DstStr ++;
DataLen ++;
continue;
}
}
ChrLen = mbtowc(DstStr, SrcStr, MB_LEN_MAX);
if (ChrLen < 0)
{
*DstStr = L'?';
ChrLen = 1;
}
SrcStr += ChrLen;
DstStr ++;
DataLen ++;
}
*DstStr = L'\0';
return;
}
static void CopySys2TagStr(wchar_t** TagStr, const SYSTEM_SHORT* System, bool Mode,
const char* CmdStr)
{
const char* DataStr;
const char* CmdPos;
const char* CmdPos2;
int ChrLen;
size_t DataLen;
const char* SrcStr;
wchar_t* DstStr;
const char* ChrStr;
UINT16 UnicodeChr;
char NCRStr[0x10];
bool DoWildcard;
if (Mode == SYSMODE_ENG)
DataStr = System->NameE;
else if (Mode == SYSMODE_JAP)
DataStr = System->NameJ;
else
return;
DataLen = strlen(DataStr);
CmdPos = System->Abbr;
while(! (*CmdPos == 0x00 || *CmdPos == '*'))
CmdPos ++;
if (*CmdPos == 0x00)
{
CmdPos = NULL;
DoWildcard = false;
}
else
{
CmdPos = CmdStr + (CmdPos - System->Abbr);
DoWildcard = true;
DataLen += strlen(CmdPos) - 0x01;
}
CmdPos2 = System->Abbr;
*TagStr = (wchar_t*)realloc(*TagStr, (DataLen + 0x01) * sizeof(wchar_t));
SrcStr = DataStr;
DstStr = *TagStr;
DataLen = 0x00;
while(SrcStr && *SrcStr)
{
if (*SrcStr == '\\')
{
SrcStr ++;
// Handle C-style Escape-Sequences (only \n is useful)
switch(*SrcStr)
{
case 'n':
*DstStr = (wchar_t)'\n';
SrcStr ++;
DstStr ++;
DataLen ++;
continue;
}
}
else if (*SrcStr == '?')
{
while(CmdPos2 != NULL && *CmdPos2 != '?')
{
if (*CmdPos2 == 0x00)
{
CmdPos2 = NULL;
break;
}
CmdPos2 ++;
}
if (CmdPos2 != NULL)
{
ChrStr = CmdStr + (CmdPos2 - System->Abbr);
ChrLen = mbtowc(DstStr, ChrStr, MB_LEN_MAX);
if (ChrLen < 0)
{
*DstStr = L'?';
ChrLen = 1;
}
SrcStr += ChrLen;
DstStr ++;
DataLen ++;
CmdPos2 ++;
continue;
}
}
else if (*SrcStr == '&')
{
// HTML NCR
ChrStr = SrcStr;
ChrStr ++;
if (*ChrStr == '#')
{
ChrStr ++;
// Format: Ӓ -> Unicode Char 01234
// ꯍ -> Unicode Char 0xABCD
UnicodeChr = 0x00;
while(*ChrStr && *ChrStr != ';')
{
if (UnicodeChr < 0x0F)
{
NCRStr[UnicodeChr] = *ChrStr;
UnicodeChr ++;
}
ChrStr ++;
}
NCRStr[UnicodeChr] = 0x00;
ChrStr ++;
if (tolower(*NCRStr) == 'x')
UnicodeChr = (UINT16)strtoul(NCRStr + 0x01, NULL, 0x10);
else
UnicodeChr = (UINT16)strtoul(NCRStr, NULL, 10);
*DstStr = (wchar_t)UnicodeChr;
SrcStr = ChrStr;
DstStr ++;
DataLen ++;
continue;
}
}
else if (DoWildcard && *SrcStr == '*')
{
SrcStr = CmdPos;
DoWildcard = false;
continue;
}
ChrLen = mbtowc(DstStr, SrcStr, MB_LEN_MAX);
if (ChrLen < 0)
{
*DstStr = L'?';
ChrLen = 1;
}
SrcStr += ChrLen;
DstStr ++;
DataLen ++;
}
*DstStr = 0x0000;
return;
}
static bool CompareSystemNames(const char* StrA, const char* StrB)
{
while(*StrA || *StrB)
{
if (*StrB == '*')
return true; // Wildcard found
if (*StrB != '?' || *StrA == '\0')
{
if (toupper(*StrA) != toupper(*StrB))
return false;
}
StrA ++;
StrB ++;
}
return true;
}
static UINT16 GetSystemString(const char* SystemName)
{
UINT16 CurSys;
CurSys = 0x00;
while(SYSTEM_NAMES[CurSys].Abbr != NULL)
{
if (CompareSystemNames(SystemName, SYSTEM_NAMES[CurSys].Abbr))
return CurSys;
CurSys ++;
}
return 0xFFFF;
}
static UINT8 TagVGM(const int ArgCount, char* ArgList[])
{
int CurArg;
char* CmdStr;
char* CmdData;
//UINT32 DataLen;
//wchar_t* TempPnt;
UINT16 CurSys;
const SYSTEM_SHORT* TempSys;
wchar_t* TempStr;
wchar_t* NoteStr;
UINT32 StrLen;
UINT32 TempLng;
UINT8 TempByt;
UINT8 RetVal;
if (! VGMHead.lngGD3Offset)
{
VGMTag.fccGD3 = 0x20336447;
VGMTag.lngVersion = 0x00000100; // Version 1.0
// The rest is done when the 1st tag is written
}
else if (VGMTag.lngVersion > 0x00000100)
{
printf("Warning! This GD3 version is NOT supported!\n");
printf("You may damage your GD3 tag!\n");
printf("Continue (Y/N) ? \t");
CurArg = toupper(getchar());
if (CurArg != 'Y')
return 0x01;
}
// Execute Commands
RetVal = 0x10; // nothing done - skip writing
if (! ArgCount)
ShowTag(0x00);
CmdStr = NULL;
for (CurArg = 0x00; CurArg < ArgCount; CurArg ++)
{
if (CmdStr != NULL)
free(CmdStr);
//CmdStr = ArgList[CurArg] + 0x01; // Skip the '-' at the beginning
CmdStr = strdup(ArgList[CurArg] + 0x01); // I need a copy, since I remove the '=' character
CmdData = strchr(CmdStr, ':');
if (CmdData != NULL)
{
*CmdData = 0x00;
CmdData ++;
}
if (! stricmp(CmdStr, "ShowTag"))
{
ShowTag(0x00);
continue;
}
else if (! stricmp(CmdStr, "ShowTagU"))
{
ShowTag(0x01);
continue;
}
else if (! stricmp(CmdStr, "ShowTag8"))
{
ShowTag(0x02);
continue;
}
if (! stricmp(CmdStr, "RmvEqual"))
{
if (CmdData != NULL)
TempByt = (toupper(CmdData[0x00]) == 'J') ? 1 : 0;
else
TempByt = 0;
printf("Removing redundant tags (default to %s): ",
TempLng ? "Eng" : "Jap");
TempLng = 0x00;
TempLng |= RemoveEqualTag(&VGMTag.strTrackNameE, &VGMTag.strTrackNameJ, TempByt) << 0;
TempLng |= RemoveEqualTag(&VGMTag.strGameNameE, &VGMTag.strGameNameJ, TempByt) << 1;
TempLng |= RemoveEqualTag(&VGMTag.strSystemNameE, &VGMTag.strSystemNameJ, TempByt) << 2;
TempLng |= RemoveEqualTag(&VGMTag.strAuthorNameE, &VGMTag.strAuthorNameJ, TempByt) << 3;
if (TempLng)
{
if (TempLng & 0x01)
printf("Title, ");
if (TempLng & 0x02)
printf("Game, ");
if (TempLng & 0x04)
printf("System, ");
if (TempLng & 0x08)
printf("Author, ");
printf("\b\b ");
RetVal = 0x00;
}
printf("\n");
continue;
}
else if (! stricmp(CmdStr, "RmvUnknown"))
{
printf("Removing \"Unknown Author\" tag: ");
TempLng = 0x00;
TempLng |= RemoveUnknown(&VGMTag.strAuthorNameE) << 0;
TempLng |= RemoveUnknown(&VGMTag.strAuthorNameJ) << 1;
if (TempLng)
{
if (TempLng & 0x01)
printf("AuthorE, ");
if (TempLng & 0x02)
printf("AuthorJ, ");
printf("\b\b ");
RetVal = 0x00;
}
printf("\n");
continue;
}
else if (! stricmp(CmdStr, "TitleCase"))
{
wchar_t* tag;
int prevCType;
printf("Applying Title Case: Title\n");
if (VGMTag.strTrackNameJ != NULL && wcslen(VGMTag.strTrackNameJ) > 0)
{
printf("Not backing up TitleE tag to TitleJ due to TitleJ being set!\n");
}
else
{
wchar_t* str = VGMTag.strTrackNameJ;
VGMTag.strTrackNameJ = VGMTag.strTrackNameE;
VGMTag.strTrackNameE = (wchar_t*)realloc(str, (wcslen(VGMTag.strTrackNameJ) + 0x01) * sizeof(wchar_t));
wcscpy(VGMTag.strTrackNameE, VGMTag.strTrackNameJ);
}
tag = VGMTag.strTrackNameE;
StrLen = wcslen(tag);
prevCType = 0;
for (TempLng = 0; TempLng < StrLen; TempLng ++)
{
if (tag[TempLng] == L'(') break;
if (iswalnum(tag[TempLng]))
{
if (prevCType == 0)
tag[TempLng] = toupper(tag[TempLng]);
else if (prevCType == 1)
tag[TempLng] = towlower(tag[TempLng]);
prevCType = iswalpha(tag[TempLng]) ? 1 : 2; // 1 - letter, 2 - number
}
else
{
prevCType = 0; // 0 = punctuation
}
}
RetVal = 0x00;
continue;
}
else if (! stricmp(CmdStr, "NotesStripAt"))
{
if (VGMTag.strNotes == NULL)
continue;
// convert parameter to UTF-16
NoteStr = NULL;
Copy2TagStr(&NoteStr, CmdData);
// search for parameter in Notes tag
TempStr = (VGMTag.strNotes != NULL) ? wcsstr(VGMTag.strNotes, NoteStr) : NULL;
if (TempStr == NULL)
{
printf("Notes Strip: \"%s\" not found.\n", CmdData);
continue;
}
// found parameter - now go back to trim off additional whitespace
while(TempStr > VGMTag.strNotes && iswspace(TempStr[-1]))
TempStr --;
*TempStr = '\0'; // set '\0' to terminate the string here
printf("Notes Strip: \"%s\" removed.\n", CmdData);
RetVal = 0x00;
continue;
}
RetVal = 0x00;
if (! stricmp(CmdStr, "RemoveTag"))
{
printf("Tag removed.\n");
if (VGMHead.lngGD3Offset)
{
VGMHead.lngEOFOffset = VGMDataLen;
VGMHead.lngGD3Offset = 0x00;
}
break;
}
if (! VGMHead.lngGD3Offset)
{
printf("Tag created.\n");
VGMHead.lngGD3Offset = VGMDataLen;
VGMTag.strTrackNameE = NULL;
VGMTag.strTrackNameJ = NULL;
VGMTag.strGameNameE = NULL;
VGMTag.strGameNameJ = NULL;
VGMTag.strSystemNameE = NULL;
VGMTag.strSystemNameJ = NULL;
VGMTag.strAuthorNameE = NULL;
VGMTag.strAuthorNameJ = NULL;
VGMTag.strReleaseDate = NULL;
VGMTag.strCreator = NULL;
VGMTag.strNotes = NULL;
Copy2TagStr(&VGMTag.strTrackNameE, "");
Copy2TagStr(&VGMTag.strTrackNameJ, "");
Copy2TagStr(&VGMTag.strGameNameE, "");
Copy2TagStr(&VGMTag.strGameNameJ, "");
Copy2TagStr(&VGMTag.strSystemNameE, "");
Copy2TagStr(&VGMTag.strSystemNameJ, "");
Copy2TagStr(&VGMTag.strAuthorNameE, "");
Copy2TagStr(&VGMTag.strAuthorNameJ, "");
Copy2TagStr(&VGMTag.strReleaseDate, "");
Copy2TagStr(&VGMTag.strCreator, "");
Copy2TagStr(&VGMTag.strNotes, "");
}
if (! stricmp(CmdStr, "ClearTag"))
{
printf("Tag cleared.\n");
Copy2TagStr(&VGMTag.strTrackNameE, "");
Copy2TagStr(&VGMTag.strTrackNameJ, "");
Copy2TagStr(&VGMTag.strGameNameE, "");
Copy2TagStr(&VGMTag.strGameNameJ, "");
Copy2TagStr(&VGMTag.strSystemNameE, "");
Copy2TagStr(&VGMTag.strSystemNameJ, "");
Copy2TagStr(&VGMTag.strAuthorNameE, "");
Copy2TagStr(&VGMTag.strAuthorNameJ, "");
Copy2TagStr(&VGMTag.strReleaseDate, "");
Copy2TagStr(&VGMTag.strCreator, "");
Copy2TagStr(&VGMTag.strNotes, "");
}
else
{
if (CmdData != NULL)
printf("Set %s = %s\n", CmdStr, CmdData);
else
printf("Set %s\n", CmdStr);
if (! stricmp(CmdStr, "Title"))
{
Copy2TagStr(&VGMTag.strTrackNameE, CmdData);
}
else if (! stricmp(CmdStr, "TitleJ"))
{
Copy2TagStr(&VGMTag.strTrackNameJ, CmdData);