-
Notifications
You must be signed in to change notification settings - Fork 31
/
clHCA.cpp
1473 lines (1365 loc) · 59.6 KB
/
clHCA.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
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
//--------------------------------------------------
// インクルード
//--------------------------------------------------
#include "clHCA.h"
#include <stdio.h>
#include <memory.h>
//--------------------------------------------------
// インライン関数
//--------------------------------------------------
inline short bswap(short v) { short r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned short bswap(unsigned short v) { unsigned short r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline int bswap(int v) { int r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned int bswap(unsigned int v) { unsigned int r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline long long bswap(long long v) { long long r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned long long bswap(unsigned long long v) { unsigned long long r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline float bswap(float v) { unsigned int i = bswap(*(unsigned int *)&v); return *(float *)&i; }
inline unsigned int ceil2(unsigned int a, unsigned int b) { return (b>0) ? (a / b + ((a%b) ? 1 : 0)) : 0; }
//--------------------------------------------------
// コンストラクタ
//--------------------------------------------------
clHCA::clHCA(unsigned int ciphKey1, unsigned int ciphKey2) :
_ciph_key1(ciphKey1), _ciph_key2(ciphKey2), _ath(), _cipher() {}
//--------------------------------------------------
// HCAチェック
//--------------------------------------------------
bool clHCA::CheckFile(void *data, unsigned int size) {
return (data&&size >= 4 && (*(unsigned int *)data & 0x7F7F7F7F) == 0x00414348);
}
//--------------------------------------------------
// チェックサム
//--------------------------------------------------
unsigned short clHCA::CheckSum(void *data, int size, unsigned short sum) {
static unsigned short v[] = {
0x0000,0x8005,0x800F,0x000A,0x801B,0x001E,0x0014,0x8011,0x8033,0x0036,0x003C,0x8039,0x0028,0x802D,0x8027,0x0022,
0x8063,0x0066,0x006C,0x8069,0x0078,0x807D,0x8077,0x0072,0x0050,0x8055,0x805F,0x005A,0x804B,0x004E,0x0044,0x8041,
0x80C3,0x00C6,0x00CC,0x80C9,0x00D8,0x80DD,0x80D7,0x00D2,0x00F0,0x80F5,0x80FF,0x00FA,0x80EB,0x00EE,0x00E4,0x80E1,
0x00A0,0x80A5,0x80AF,0x00AA,0x80BB,0x00BE,0x00B4,0x80B1,0x8093,0x0096,0x009C,0x8099,0x0088,0x808D,0x8087,0x0082,
0x8183,0x0186,0x018C,0x8189,0x0198,0x819D,0x8197,0x0192,0x01B0,0x81B5,0x81BF,0x01BA,0x81AB,0x01AE,0x01A4,0x81A1,
0x01E0,0x81E5,0x81EF,0x01EA,0x81FB,0x01FE,0x01F4,0x81F1,0x81D3,0x01D6,0x01DC,0x81D9,0x01C8,0x81CD,0x81C7,0x01C2,
0x0140,0x8145,0x814F,0x014A,0x815B,0x015E,0x0154,0x8151,0x8173,0x0176,0x017C,0x8179,0x0168,0x816D,0x8167,0x0162,
0x8123,0x0126,0x012C,0x8129,0x0138,0x813D,0x8137,0x0132,0x0110,0x8115,0x811F,0x011A,0x810B,0x010E,0x0104,0x8101,
0x8303,0x0306,0x030C,0x8309,0x0318,0x831D,0x8317,0x0312,0x0330,0x8335,0x833F,0x033A,0x832B,0x032E,0x0324,0x8321,
0x0360,0x8365,0x836F,0x036A,0x837B,0x037E,0x0374,0x8371,0x8353,0x0356,0x035C,0x8359,0x0348,0x834D,0x8347,0x0342,
0x03C0,0x83C5,0x83CF,0x03CA,0x83DB,0x03DE,0x03D4,0x83D1,0x83F3,0x03F6,0x03FC,0x83F9,0x03E8,0x83ED,0x83E7,0x03E2,
0x83A3,0x03A6,0x03AC,0x83A9,0x03B8,0x83BD,0x83B7,0x03B2,0x0390,0x8395,0x839F,0x039A,0x838B,0x038E,0x0384,0x8381,
0x0280,0x8285,0x828F,0x028A,0x829B,0x029E,0x0294,0x8291,0x82B3,0x02B6,0x02BC,0x82B9,0x02A8,0x82AD,0x82A7,0x02A2,
0x82E3,0x02E6,0x02EC,0x82E9,0x02F8,0x82FD,0x82F7,0x02F2,0x02D0,0x82D5,0x82DF,0x02DA,0x82CB,0x02CE,0x02C4,0x82C1,
0x8243,0x0246,0x024C,0x8249,0x0258,0x825D,0x8257,0x0252,0x0270,0x8275,0x827F,0x027A,0x826B,0x026E,0x0264,0x8261,
0x0220,0x8225,0x822F,0x022A,0x823B,0x023E,0x0234,0x8231,0x8213,0x0216,0x021C,0x8219,0x0208,0x820D,0x8207,0x0202,
};
for (unsigned char *s = (unsigned char *)data, *e = s + size; s<e; s++)sum = (sum << 8) ^ v[(sum >> 8) ^ *s];
return sum;
}
//--------------------------------------------------
// ヘッダ情報をコンソール出力
//--------------------------------------------------
bool clHCA::PrintInfo(const char *filenameHCA) {
// チェック
if (!(filenameHCA))return false;
// HCAファイルを開く
FILE *fp;
if (fopen_s(&fp, filenameHCA, "rb")) {
printf("Error: ファイルが開けませんでした。\n");
return false;
}
// ヘッダチェック
stHeader header;
memset(&header, 0, sizeof(header));
fread(&header, sizeof(header), 1, fp);
if (!CheckFile(&header, sizeof(header))) {
printf("Error: HCAファイルではありません。\n");
fclose(fp); return false;
}
// ヘッダ解析
header.dataOffset = bswap(header.dataOffset);
unsigned char *data = new unsigned char[header.dataOffset];
if (!data) {
printf("Error: メモリ不足です。\n");
fclose(fp); return false;
}
fseek(fp, 0, SEEK_SET);
fread(data, header.dataOffset, 1, fp);
unsigned char *s = (unsigned char *)data;
unsigned int size = header.dataOffset;
// サイズチェック
if (size<sizeof(stHeader)) {
printf("Error: ヘッダのサイズが小さすぎます。\n");
delete[] data; fclose(fp); return false;
}
// HCA
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00414348) {
stHeader *hca = (stHeader *)s; s += sizeof(stHeader);
_version = bswap(hca->version);
_dataOffset = bswap(hca->dataOffset);
printf("コーデック: HCA\n");
printf("バージョン: %d.%d\n", _version >> 8, _version & 0xFF);
//if(size<_dataOffset)return false;
if (CheckSum(hca, _dataOffset))printf("※ ヘッダが破損しています。改変してる場合もこの警告が出ます。\n");
}
else {
printf("※ HCAチャンクがありません。再生に必要な情報です。\n");
}
// fmt
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00746D66) {
stFormat *fmt = (stFormat *)s; s += sizeof(stFormat);
_channelCount = fmt->channelCount;
_samplingRate = bswap(fmt->samplingRate << 8);
_blockCount = bswap(fmt->blockCount);
_muteHeader = bswap(fmt->muteHeader);
_muteFooter = bswap(fmt->muteFooter);
switch (_channelCount) {
case 1:printf("チャンネル数: モノラル (1チャンネル)\n"); break;
case 2:printf("チャンネル数: ステレオ (2チャンネル)\n"); break;
default:printf("チャンネル数: %dチャンネル\n", _channelCount); break;
}
if (!(_channelCount >= 1 && _channelCount <= 16)) {
printf("※ チャンネル数の範囲は1~16です。\n");
}
printf("サンプリングレート: %dHz\n", _samplingRate);
if (!(_samplingRate >= 1 && _samplingRate <= 0x7FFFFF)) {
printf("※ サンプリングレートの範囲は1~8388607(0x7FFFFF)です。\n");
}
printf("ブロック数: %d\n", _blockCount);
printf("先頭無音ブロック数: %d\n", (_muteHeader - 0x80) / 0x400);
printf("末尾無音サンプル数: %d\n", _muteFooter);
}
else {
printf("※ fmtチャンクがありません。再生に必要な情報です。\n");
}
// comp
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706D6F63) {
stCompress *comp = (stCompress *)s; s += sizeof(stCompress);
_blockSize = bswap(comp->blockSize);
_comp_r01 = comp->r01;
_comp_r02 = comp->r02;
_comp_r03 = comp->r03;
_comp_r04 = comp->r04;
_comp_r05 = comp->r05;
_comp_r06 = comp->r06;
_comp_r07 = comp->r07;
_comp_r08 = comp->r08;
unsigned int bps = _samplingRate*_blockSize / 128;
if (bps<1000000)printf("ビットレート: %gkbps CBR (固定ビットレート)\n", bps / 1000.0f);
else printf("ビットレート: %gMbps CBR (固定ビットレート)\n", bps / 1000000.0f);
printf("ブロックサイズ: 0x%X\n", _blockSize);
if (!(_blockSize >= 8 && _blockSize <= 0xFFFF)) {
printf("※ ブロックサイズの範囲は8~65535(0xFFFF)です。v1.3では0でVBRになるようになってましたが、v2.0から廃止されたようです。\n");
}
printf("comp1: %d\n", _comp_r01);
printf("comp2: %d\n", _comp_r02);
if (!(_comp_r01 >= 0 && _comp_r01 <= _comp_r02&&_comp_r02 <= 0x1F)) {
printf("※ comp1とcomp2の範囲は0<=comp1<=comp2<=31です。v2.0現在、comp1は1、comp2は15で固定されています。\n");
}
printf("comp3: %d\n", _comp_r03);
if (!_comp_r03) {
printf("※ comp3は1以上の値です。\n");
}
printf("comp4: %d\n", _comp_r04);
printf("comp5: %d\n", _comp_r05);
printf("comp6: %d\n", _comp_r06);
printf("comp7: %d\n", _comp_r07);
printf("comp8: %d\n", _comp_r08);
}
// dec
else if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00636564) {
stDecode *dec = (stDecode *)s; s += sizeof(stDecode);
_blockSize = bswap(dec->blockSize);
_comp_r01 = dec->r01;
_comp_r02 = dec->r02;
_comp_r03 = dec->r04;
_comp_r04 = dec->r03;
_comp_r05 = dec->count1 + 1;
_comp_r06 = ((dec->enableCount2) ? dec->count2 : dec->count1) + 1;
_comp_r07 = _comp_r05 - _comp_r06;
_comp_r08 = 0;
unsigned int bps = _samplingRate*_blockSize / 128;
if (bps<1000000)printf("ビットレート: %gkbps CBR (固定ビットレート)\n", bps / 1000.0f);
else printf("ビットレート: %gMbps CBR (固定ビットレート)\n", bps / 1000000.0f);
printf("ブロックサイズ: 0x%X\n", _blockSize);
if (!(_blockSize >= 8 && _blockSize <= 0xFFFF)) {
printf("※ ブロックサイズの範囲は8~65535(0xFFFF)です。v1.3では0でVBRになるようになってましたが、v2.0から廃止されたようです。\n");
}
printf("dec1: %d\n", _comp_r01);
printf("dec2: %d\n", _comp_r02);
if (!(_comp_r01 >= 0 && _comp_r01 <= _comp_r02&&_comp_r02 <= 0x1F)) {
printf("※ dec1とdec2の範囲は0<=dec1<=dec2<=31です。v2.0現在、dec1は1、dec2は15で固定されています。\n");
}
printf("dec3: %d\n", _comp_r03);
if (!_comp_r03) {
printf("※ dec3は再生時に1以上の値に修正されます。\n");
}
printf("dec4: %d\n", _comp_r04);
printf("dec5: %d\n", _comp_r05);
printf("dec6: %d\n", _comp_r06);
printf("dec7: %d\n", _comp_r07);
}
else {
printf("※ compチャンクまたはdecチャンクがありません。再生に必要な情報です。\n");
}
// vbr
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00726276) {
stVBR *vbr = (stVBR *)s; s += sizeof(stVBR);
_vbr_r01 = bswap(vbr->r01);
_vbr_r02 = bswap(vbr->r02);
printf("ビットレート: VBR (可変ビットレート) ※v2.0で廃止されています。\n");
if (!(_blockSize == 0)) {
printf("※ compまたはdecチャンクですでにCBRが指定されています。\n");
}
printf("vbr1: %d\n", _vbr_r01);
if (!(_vbr_r01 >= 0 && _vbr_r01 <= 0x1FF)) {
printf("※ vbr1の範囲は0~511(0x1FF)です。\n");
}
printf("vbr2: %d\n", _vbr_r02);
}
else {
_vbr_r01 = 0;
_vbr_r02 = 0;
}
// ath
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00687461) {
stATH *ath = (stATH *)s; s += 6;//s+=sizeof(stATH);
_ath_type = ath->type;
printf("ATHタイプ:%d ※v2.0から廃止されています。\n", _ath_type);
}
else {
if (_version<0x200) {
printf("ATHタイプ:1 ※v2.0から廃止されています。\n");
}
}
// loop
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706F6F6C) {
stLoop *loop = (stLoop *)s; s += sizeof(stLoop);
_loopStart = bswap(loop->start);
_loopEnd = bswap(loop->end);
_loopCount = bswap(loop->count);
_loop_r01 = bswap(loop->r01);
printf("ループ開始ブロック: %d\n", _loopStart);
printf("ループ終了ブロック: %d\n", _loopEnd);
if (!(_loopStart >= 0 && _loopStart <= _loopEnd&&_loopEnd<_blockCount)) {
printf("※ ループ開始ブロックとループ終了ブロックの範囲は、0<=ループ開始ブロック<=ループ終了ブロック<ブロック数 です。\n");
}
if (_loopCount == 0x80) {
printf("ループ回数: 無限ループ\n");
}
else {
printf("ループ回数: %d回\n", _loopCount);
}
printf("ループ情報1: %d\n", _loop_r01);
}
// ciph
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x68706963) {
stCipher *ciph = (stCipher *)s; s += 6;//s+=sizeof(stCipher);
_ciph_type = bswap(ciph->type);
switch (_ciph_type) {
case 0:printf("暗号化タイプ: なし\n"); break;
case 1:printf("暗号化タイプ: 鍵無し暗号化\n"); break;
case 0x38:printf("暗号化タイプ: 鍵有り暗号化 ※正しい鍵を使わないと出力波形がおかしくなります。\n"); break;
default:printf("暗号化タイプ: %d\n", _ciph_type); break;
}
if (!(_ciph_type == 0 || _ciph_type == 1 || _ciph_type == 0x38)) {
printf("※ この暗号化タイプは、v2.0現在再生できません。\n");
}
}
// rva
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00617672) {
stRVA *rva = (stRVA *)s; s += sizeof(stRVA);
_rva_volume = bswap(rva->volume);
printf("相対ボリューム調節: %g倍\n", _rva_volume);
}
// comm
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x6D6D6F63) {
stComment *comm = (stComment *)s; s += 5;//s+=sizeof(stComment);
_comm_len = comm->len;
_comm_comment = (char *)s;
printf("コメント: %s\n", _comm_comment);
}
delete[] data;
// 閉じる
fclose(fp);
return true;
}
//--------------------------------------------------
// 復号化
//--------------------------------------------------
bool clHCA::Decrypt(const char *filenameHCA) {
// チェック
if (!(filenameHCA))return false;
// HCAファイルを開く
FILE *fp;
if (fopen_s(&fp, filenameHCA, "r+b"))return false;
// ヘッダチェック
stHeader header;
memset(&header, 0, sizeof(header));
fread(&header, sizeof(header), 1, fp);
if (!CheckFile(&header, sizeof(header))) { fclose(fp); return false; }
// ヘッダ解析
header.dataOffset = bswap(header.dataOffset);
unsigned char *data = new unsigned char[header.dataOffset];
if (!data) { fclose(fp); return false; }
fseek(fp, 0, SEEK_SET);
fread(data, header.dataOffset, 1, fp);
unsigned char *s = (unsigned char *)data;
unsigned int size = header.dataOffset;
// サイズチェック
if (size<sizeof(stHeader)) { delete[] data; fclose(fp); return false; }
// HCA
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00414348) {
stHeader *hca = (stHeader *)s; s += sizeof(stHeader);
hca->hca &= 0x7F7F7F7F;
_version = bswap(hca->version);
_dataOffset = bswap(hca->dataOffset);
}
else {
delete[] data; fclose(fp); return false;
}
// fmt
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00746D66) {
stFormat *fmt = (stFormat *)s; s += sizeof(stFormat);
fmt->fmt &= 0x7F7F7F7F;
_samplingRate = bswap(fmt->samplingRate << 8);
_blockCount = bswap(fmt->blockCount);
}
else {
delete[] data; fclose(fp); return false;
}
// comp
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706D6F63) {
stCompress *comp = (stCompress *)s; s += sizeof(stCompress);
comp->comp &= 0x7F7F7F7F;
_blockSize = bswap(comp->blockSize);
}
// dec
else if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00636564) {
stDecode *dec = (stDecode *)s; s += sizeof(stDecode);
dec->dec &= 0x7F7F7F7F;
_blockSize = bswap(dec->blockSize);
}
else {
delete[] data; fclose(fp); return false;
}
// vbr
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00726276) {
stVBR *vbr = (stVBR *)s; s += sizeof(stVBR);
vbr->vbr &= 0x7F7F7F7F;
}
// ath
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00687461) {
stATH *ath = (stATH *)s; s += 6;//s+=sizeof(stATH);
ath->ath &= 0x7F7F7F7F;
_ath_type = bswap(ath->type);
//ath->type=0;
}
else {
if (_version<0x200)_ath_type = 1;
}
// loop
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706F6F6C) {
stLoop *loop = (stLoop *)s; s += sizeof(stLoop);
loop->loop &= 0x7F7F7F7F;
}
// ciph
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x68706963) {
stCipher *ciph = (stCipher *)s; s += 6;//s+=sizeof(stCipher);
ciph->ciph &= 0x7F7F7F7F;
_ciph_type = bswap(ciph->type);
ciph->type = 0;
}
// rva
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00617672) {
stRVA *rva = (stRVA *)s; s += sizeof(stRVA);
rva->rva &= 0x7F7F7F7F;
}
// comm
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x6D6D6F63) {
stComment *comm = (stComment *)s; s += 5;//s+=sizeof(stComment);
comm->comm &= 0x7F7F7F7F;
s += comm->len;
}
// pad
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00646170) {
stPadding *pad = (stPadding *)s; s += sizeof(stPadding);
pad->pad &= 0x7F7F7F7F;
}
// 初期化
if (!_ath.Init(_ath_type, _samplingRate)) { delete[] data; fclose(fp); return false; }
if (!_cipher.Init(_ciph_type, _ciph_key1, _ciph_key2)) { delete[] data; fclose(fp); return false; }
unsigned char *data2 = new unsigned char[_blockSize];
if (!data2) { delete[] data; fclose(fp); return false; }
// ヘッダを書き込み
*(unsigned short *)&data[size - 2] = bswap(CheckSum(data, size - 2));
fseek(fp, 0, SEEK_SET);
fwrite(data, size, 1, fp);
delete[] data;
// ブロックデータを復号化
if (_ciph_type != 0) {
for (unsigned int i = 0, a = size; i<_blockCount; i++, a += _blockSize) {
fseek(fp, a, SEEK_SET);
fread(data2, _blockSize, 1, fp);
_cipher.Mask(data2, _blockSize);
*(unsigned short *)&data2[_blockSize - 2] = bswap(CheckSum(data2, _blockSize - 2));
fseek(fp, a, SEEK_SET);
fwrite(data2, _blockSize, 1, fp);
}
}
delete[] data2;
// 閉じる
fclose(fp);
return true;
}
//--------------------------------------------------
// デコードしてWAVEファイルに保存
//--------------------------------------------------
bool clHCA::DecodeToWavefile(const char *filenameHCA, const char *filenameWAV, float volume, int mode, int loop) {
// チェック
if (!(filenameHCA))return false;
// HCAファイルを開く
FILE *fp;
if (fopen_s(&fp, filenameHCA, "rb"))return false;
// 保存
if (!DecodeToWavefileStream(fp, filenameWAV, volume, mode, loop)) { fclose(fp); return false; }
// 閉じる
fclose(fp);
return true;
}
bool clHCA::DecodeToWavefileStream(void *fpHCA, const char *filenameWAV, float volume, int mode, int loop) {
// チェック
if (!(fpHCA&&filenameWAV && (mode == 0 || mode == 8 || mode == 16 || mode == 24 || mode == 32) && loop >= 0))return false;
//
FILE *fp1 = (FILE *)fpHCA;
unsigned int address = ftell(fp1);
// ヘッダチェック
stHeader header;
memset(&header, 0, sizeof(header));
fread(&header, sizeof(header), 1, fp1);
if (!CheckFile(&header, sizeof(header)))return false;
// ヘッダ解析
header.dataOffset = bswap(header.dataOffset);
unsigned char *data1 = new unsigned char[header.dataOffset];
if (!data1) { fclose(fp1); return false; }
fseek(fp1, address, SEEK_SET);
fread(data1, header.dataOffset, 1, fp1);
if (!Decode(data1, header.dataOffset, 0)) { delete[] data1; return false; }
// WAVEファイルを開く
FILE *fp2;
if (fopen_s(&fp2, filenameWAV, "wb")) { delete[] data1; return false; }
// WAVEヘッダを書き込み
struct stWAVEHeader {
char riff[4];
unsigned int riffSize;
char wave[4];
char fmt[4];
unsigned int fmtSize;
unsigned short fmtType;
unsigned short fmtChannelCount;
unsigned int fmtSamplingRate;
unsigned int fmtSamplesPerSec;
unsigned short fmtSamplingSize;
unsigned short fmtBitCount;
}wavRiff = { 'R','I','F','F',0,'W','A','V','E','f','m','t',' ',0x10,0,0,0,0,0,0 };
struct stWAVEsmpl {
char smpl[4];
unsigned int smplSize;
unsigned int manufacturer;
unsigned int product;
unsigned int samplePeriod;
unsigned int MIDIUnityNote;
unsigned int MIDIPitchFraction;
unsigned int SMPTEFormat;
unsigned int SMPTEOffset;
unsigned int sampleLoops;
unsigned int samplerData;
unsigned int loop_Identifier;
unsigned int loop_Type;
unsigned int loop_Start;
unsigned int loop_End;
unsigned int loop_Fraction;
unsigned int loop_PlayCount;
}wavSmpl = { 's','m','p','l',0x3C,0,0,0,0x3C,0,0,0,1,0x18,0,0,0,0,0,0 };
struct stWAVEnote {
char note[4];
unsigned int noteSize;
unsigned int dwName;
}wavNote = { 'n','o','t','e',0,0 };
struct stWAVEdata {
char data[4];
unsigned int dataSize;
}wavData = { 'd','a','t','a',0 };
wavRiff.fmtType = (mode>0) ? 1 : 3;
wavRiff.fmtChannelCount = _channelCount;
wavRiff.fmtBitCount = (mode>0) ? mode : 32;
wavRiff.fmtSamplingRate = _samplingRate;
wavRiff.fmtSamplingSize = wavRiff.fmtBitCount / 8 * wavRiff.fmtChannelCount;
wavRiff.fmtSamplesPerSec = wavRiff.fmtSamplingRate*wavRiff.fmtSamplingSize;
if (_loopFlg) {
wavSmpl.samplePeriod = (unsigned int)(1 / (double)wavRiff.fmtSamplingRate * 1000000000);
wavSmpl.loop_Start = _loopStart * 0x80 * 8 + _muteFooter;//※計算方法不明
wavSmpl.loop_End = (_loopEnd + 1) * 0x80 * 8 - 1;//※計算方法不明
wavSmpl.loop_PlayCount = (_loopCount == 0x80) ? 0 : _loopCount;
}
else if (loop) {
wavSmpl.loop_Start = 0;
wavSmpl.loop_End = (_blockCount + 1) * 0x80 * 8 - 1;//※計算方法不明
_loopStart = 0;
_loopEnd = _blockCount;
}
if (_comm_comment) {
wavNote.noteSize = 4 + _comm_len + 1;
if (wavNote.noteSize & 3)wavNote.noteSize += 4 - (wavNote.noteSize & 3);
}
wavData.dataSize = _blockCount * 0x80 * 8 * wavRiff.fmtSamplingSize + (wavSmpl.loop_End - wavSmpl.loop_Start)*loop;
wavRiff.riffSize = 0x1C + ((_loopFlg && !loop) ? sizeof(wavSmpl) : 0) + (_comm_comment ? 8 + wavNote.noteSize : 0) + sizeof(wavData) + wavData.dataSize;
fwrite(&wavRiff, sizeof(wavRiff), 1, fp2);
if (_loopFlg && !loop)fwrite(&wavSmpl, sizeof(wavSmpl), 1, fp2);
if (_comm_comment) {
int address = ftell(fp2);
fwrite(&wavNote, sizeof(wavNote), 1, fp2);
fputs(_comm_comment, fp2);
fseek(fp2, address + 8 + wavNote.noteSize, SEEK_SET);
}
fwrite(&wavData, sizeof(wavData), 1, fp2);
// 相対ボリュームを調節
_rva_volume *= volume;
// デコード
void *modeFunction = DecodeToWavefile_DecodeMode16bit;
switch (mode) {
case 0:modeFunction = DecodeToWavefile_DecodeModeFloat; break;
case 8:modeFunction = DecodeToWavefile_DecodeMode8bit; break;
case 16:modeFunction = DecodeToWavefile_DecodeMode16bit; break;
case 24:modeFunction = DecodeToWavefile_DecodeMode24bit; break;
case 32:modeFunction = DecodeToWavefile_DecodeMode32bit; break;
}
unsigned char *data2 = new unsigned char[_blockSize];
if (!data2) { delete[] data1; fclose(fp2); return false; }
if (!loop) {
if (!DecodeToWavefile_Decode(fp1, fp2, address + _dataOffset, _blockCount, data2, modeFunction)) { delete[] data2; delete[] data1; fclose(fp2); return false; }
}
else {
unsigned int loopBlockOffset = _dataOffset + _loopStart*_blockSize;
unsigned int loopBlockCount = _loopEnd - _loopStart;
if (!DecodeToWavefile_Decode(fp1, fp2, address + _dataOffset, _loopEnd, data2, modeFunction)) { delete[] data2; delete[] data1; fclose(fp2); return false; }
for (int i = 1; i<loop; i++) {
if (!DecodeToWavefile_Decode(fp1, fp2, address + loopBlockOffset, loopBlockCount, data2, modeFunction)) { delete[] data2; delete[] data1; fclose(fp2); return false; }
}
if (!DecodeToWavefile_Decode(fp1, fp2, address + loopBlockOffset, _blockCount - _loopStart, data2, modeFunction)) { delete[] data2; delete[] data1; fclose(fp2); return false; }
}
delete[] data2;
delete[] data1;
// 閉じる
fclose(fp2);
return true;
}
bool clHCA::DecodeToWavefile_Decode(void *fp1, void *fp2, unsigned int address, unsigned int count, void *data, void *modeFunction) {
float f;
fseek((FILE *)fp1, address, SEEK_SET);
for (unsigned int l = 0; l<count; l++, address += _blockSize) {
fread(data, _blockSize, 1, (FILE *)fp1);
if (!Decode(data, _blockSize, address))return false;
for (int i = 0; i<8; i++) {
for (int j = 0; j<0x80; j++) {
for (unsigned int k = 0; k<_channelCount; k++) {
f = _channel[k].wave[i][j] * _rva_volume;
if (f>1) { f = 1; }
else if (f<-1) { f = -1; }
((void(*)(float, void *))modeFunction)(f, fp2);
}
}
}
}
return true;
}
void clHCA::DecodeToWavefile_DecodeModeFloat(float f, void *fp) { fwrite(&f, sizeof(f), 1, (FILE *)fp); }
void clHCA::DecodeToWavefile_DecodeMode8bit(float f, void *fp) { int v = (int)(f * 0x7F) + 0x80; fwrite(&v, 1, 1, (FILE *)fp); }
void clHCA::DecodeToWavefile_DecodeMode16bit(float f, void *fp) { int v = (int)(f * 0x7FFF); fwrite(&v, 2, 1, (FILE *)fp); }
void clHCA::DecodeToWavefile_DecodeMode24bit(float f, void *fp) { int v = (int)(f * 0x7FFFFF); fwrite(&v, 3, 1, (FILE *)fp); }
void clHCA::DecodeToWavefile_DecodeMode32bit(float f, void *fp) { int v = (int)(f * 0x7FFFFFFF); fwrite(&v, 4, 1, (FILE *)fp); }
//--------------------------------------------------
// エンコードしてHCAファイルに保存
//--------------------------------------------------
/*bool clHCA::EncodeFromWavefile(const char *filenameWAV,const char *filenameHCA,float volume){
// チェック
if(!(filenameWAV))return false;
// WAVファイルを開く
FILE *fp;
if(fopen_s(&fp,filenameWAV,"rb"))return false;
// 保存
if(!EncodeFromWavefileStream(fp,filenameHCA,volume)){fclose(fp);return false;}
// 閉じる
fclose(fp);
return true;
}
bool clHCA::EncodeFromWavefileStream(void *fpWAV,const char *filenameHCA,float volume){
// チェック
if(!(fpWAV&&filenameHCA))return false;
//
FILE *fp1=(FILE *)fpWAV;
unsigned int address=ftell(fp1);
// ヘッダチェック
struct stWAVEHeader{
unsigned int riff;
unsigned int riffSize;
unsigned int wave;
unsigned int fmt;
unsigned int fmtSize;
unsigned short fmtType;
unsigned short fmtChannelCount;
unsigned int fmtSamplingRate;
unsigned int fmtSamplesPerSec;
unsigned short fmtSamplingSize;
unsigned short fmtBitCount;
}header;
memset(&header,0,sizeof(header));
fread(&header,sizeof(header),1,fp1);
if(!(header.riff==0x46464952&&header.wave==0x45564157&&header.fmt==0x20746D66&&(header.fmtType==1||header.fmtType==3)))return false;
//@@@@@@@@@@@@@@@@@@@@@@@
return true;
}*/
//--------------------------------------------------
// ATH
//--------------------------------------------------
clHCA::clATH::clATH() { Init0(); }
bool clHCA::clATH::Init(int type, unsigned int key) {
switch (type) {
case 0:Init0(); break;
case 1:Init1(key); break;
default:return false;
}
return true;
}
unsigned char *clHCA::clATH::GetTable(void) {
return _table;
}
void clHCA::clATH::Init0(void) {
memset(_table, 0, sizeof(_table));
}
void clHCA::clATH::Init1(unsigned int key) {
static unsigned char list[] = {
0x78,0x5F,0x56,0x51,0x4E,0x4C,0x4B,0x49,0x48,0x48,0x47,0x46,0x46,0x45,0x45,0x45,
0x44,0x44,0x44,0x44,0x43,0x43,0x43,0x43,0x43,0x43,0x42,0x42,0x42,0x42,0x42,0x42,
0x42,0x42,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x40,0x40,0x40,0x40,
0x40,0x40,0x40,0x40,0x40,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,
0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,
0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,
0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3B,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,0x3C,
0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3D,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3E,0x3F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x43,0x43,0x43,
0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x44,0x44,
0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x45,0x45,0x45,0x45,
0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x46,0x46,0x46,0x46,0x46,0x46,0x46,0x46,
0x46,0x46,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x47,0x48,0x48,0x48,0x48,
0x48,0x48,0x48,0x48,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0x4A,0x4A,0x4A,0x4A,
0x4A,0x4A,0x4A,0x4A,0x4B,0x4B,0x4B,0x4B,0x4B,0x4B,0x4B,0x4C,0x4C,0x4C,0x4C,0x4C,
0x4C,0x4D,0x4D,0x4D,0x4D,0x4D,0x4D,0x4E,0x4E,0x4E,0x4E,0x4E,0x4E,0x4F,0x4F,0x4F,
0x4F,0x4F,0x4F,0x50,0x50,0x50,0x50,0x50,0x51,0x51,0x51,0x51,0x51,0x52,0x52,0x52,
0x52,0x52,0x53,0x53,0x53,0x53,0x54,0x54,0x54,0x54,0x54,0x55,0x55,0x55,0x55,0x56,
0x56,0x56,0x56,0x57,0x57,0x57,0x57,0x57,0x58,0x58,0x58,0x59,0x59,0x59,0x59,0x5A,
0x5A,0x5A,0x5A,0x5B,0x5B,0x5B,0x5B,0x5C,0x5C,0x5C,0x5D,0x5D,0x5D,0x5D,0x5E,0x5E,
0x5E,0x5F,0x5F,0x5F,0x60,0x60,0x60,0x61,0x61,0x61,0x61,0x62,0x62,0x62,0x63,0x63,
0x63,0x64,0x64,0x64,0x65,0x65,0x66,0x66,0x66,0x67,0x67,0x67,0x68,0x68,0x68,0x69,
0x69,0x6A,0x6A,0x6A,0x6B,0x6B,0x6B,0x6C,0x6C,0x6D,0x6D,0x6D,0x6E,0x6E,0x6F,0x6F,
0x70,0x70,0x70,0x71,0x71,0x72,0x72,0x73,0x73,0x73,0x74,0x74,0x75,0x75,0x76,0x76,
0x77,0x77,0x78,0x78,0x78,0x79,0x79,0x7A,0x7A,0x7B,0x7B,0x7C,0x7C,0x7D,0x7D,0x7E,
0x7E,0x7F,0x7F,0x80,0x80,0x81,0x81,0x82,0x83,0x83,0x84,0x84,0x85,0x85,0x86,0x86,
0x87,0x88,0x88,0x89,0x89,0x8A,0x8A,0x8B,0x8C,0x8C,0x8D,0x8D,0x8E,0x8F,0x8F,0x90,
0x90,0x91,0x92,0x92,0x93,0x94,0x94,0x95,0x95,0x96,0x97,0x97,0x98,0x99,0x99,0x9A,
0x9B,0x9B,0x9C,0x9D,0x9D,0x9E,0x9F,0xA0,0xA0,0xA1,0xA2,0xA2,0xA3,0xA4,0xA5,0xA5,
0xA6,0xA7,0xA7,0xA8,0xA9,0xAA,0xAA,0xAB,0xAC,0xAD,0xAE,0xAE,0xAF,0xB0,0xB1,0xB1,
0xB2,0xB3,0xB4,0xB5,0xB6,0xB6,0xB7,0xB8,0xB9,0xBA,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
0xC0,0xC1,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xC9,0xCA,0xCB,0xCC,0xCD,
0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,
0xDE,0xDF,0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xED,0xEE,
0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFF,0xFF,
};
for (unsigned int i = 0, v = 0; i<0x80; i++, v += key) {
unsigned int index = v >> 13;
if (index >= 0x28E) {
memset(&_table[i], 0xFF, 0x80 - i);
break;
}
_table[i] = list[index];
}
}
//--------------------------------------------------
// 暗号化テーブル
//--------------------------------------------------
clHCA::clCipher::clCipher() { Init0(); }
bool clHCA::clCipher::Init(int type, unsigned int key1, unsigned int key2) {
if (!(key1 | key2))type = 0;
switch (type) {
case 0:Init0(); break;
case 1:Init1(); break;
case 56:Init56(key1, key2); break;
default:return false;
}
return true;
}
void clHCA::clCipher::Mask(void *data, int size) {
for (unsigned char *d = (unsigned char *)data; size>0; d++, size--)*d = _table[*d];
}
void clHCA::clCipher::Init0(void) {
for (int i = 0; i<0x100; i++)_table[i] = i;
}
void clHCA::clCipher::Init1(void) {
for (int i = 1, v = 0; i<0xFF; i++) {
v = (v * 13 + 11) & 0xFF;
if (v == 0 || v == 0xFF)v = (v * 13 + 11) & 0xFF;
_table[i] = v;
}
_table[0] = 0;
_table[0xFF] = 0xFF;
}
void clHCA::clCipher::Init56(unsigned int key1, unsigned int key2) {
// テーブル1を生成
unsigned char t1[8];
if (!key1)key2--;
key1--;
for (int i = 0; i<7; i++) {
t1[i] = key1;
key1 = (key1 >> 8) | (key2 << 24);
key2 >>= 8;
}
// テーブル2
unsigned char t2[0x10] = {
t1[1],t1[1] ^ t1[6],
t1[2] ^ t1[3],t1[2],
t1[2] ^ t1[1],t1[3] ^ t1[4],
t1[3],t1[3] ^ t1[2],
t1[4] ^ t1[5],t1[4],
t1[4] ^ t1[3],t1[5] ^ t1[6],
t1[5],t1[5] ^ t1[4],
t1[6] ^ t1[1],t1[6],
};
// テーブル3
unsigned char t3[0x100], t31[0x10], t32[0x10], *t = t3;
Init56_CreateTable(t31, t1[0]);
for (int i = 0; i<0x10; i++) {
Init56_CreateTable(t32, t2[i]);
unsigned char v = t31[i] << 4;
for (int j = 0; j<0x10; j++) {
*(t++) = v | t32[j];
}
}
// CIPHテーブル
t = &_table[1];
for (int i = 0, v = 0; i<0x100; i++) {
v = (v + 0x11) & 0xFF;
unsigned char a = t3[v];
if (a != 0 && a != 0xFF)*(t++) = a;
}
_table[0] = 0;
_table[0xFF] = 0xFF;
}
void clHCA::clCipher::Init56_CreateTable(unsigned char *r, unsigned char key) {
int mul = ((key & 1) << 3) | 5;
int add = (key & 0xE) | 1;
key >>= 4;
for (int i = 0; i<0x10; i++) {
key = (key*mul + add) & 0xF;
*(r++) = key;
}
}
//--------------------------------------------------
// データ
//--------------------------------------------------
clHCA::clData::clData(void *data, int size) :_data((unsigned char *)data), _size(size * 8 - 16), _bit(0) {}
int clHCA::clData::CheckBit(int bitSize) {
int v = 0;
if (_bit + bitSize <= _size) {
static int mask[] = { 0xFFFFFF,0x7FFFFF,0x3FFFFF,0x1FFFFF,0x0FFFFF,0x07FFFF,0x03FFFF,0x01FFFF };
unsigned char *data = &_data[_bit >> 3];
v = data[0]; v = (v << 8) | data[1]; v = (v << 8) | data[2];
v &= mask[_bit & 7];
v >>= 24 - (_bit & 7) - bitSize;
}
return v;
}
int clHCA::clData::GetBit(int bitSize) {
int v = CheckBit(bitSize);
_bit += bitSize;
return v;
}
void clHCA::clData::AddBit(int bitSize) {
_bit += bitSize;
}
//--------------------------------------------------
// デコード
//--------------------------------------------------
bool clHCA::Decode(void *data, unsigned int size, unsigned int address) {
// チェック
if (!(data))return false;
// ヘッダ
if (address == 0) {
unsigned char *s = (unsigned char *)data;
// サイズチェック
if (size<sizeof(stHeader))return false;
// HCA
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00414348) {
stHeader *hca = (stHeader *)s; s += sizeof(stHeader);
_version = bswap(hca->version);
_dataOffset = bswap(hca->dataOffset);
//if(!(_version<=0x200&&_version>0x101))return false; // バージョンチェック(無効)
if (size<_dataOffset)return false;
//if(CheckSum(hca,_dataOffset))return false; // ヘッダの破損チェック(ヘッダ改変を有効にするため破損チェック無効)
}
else {
return false;
}
// fmt
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00746D66) {
stFormat *fmt = (stFormat *)s; s += sizeof(stFormat);
_channelCount = fmt->channelCount;
_samplingRate = bswap(fmt->samplingRate << 8);
_blockCount = bswap(fmt->blockCount);
_muteHeader = bswap(fmt->muteHeader);
_muteFooter = bswap(fmt->muteFooter);
if (!(_channelCount >= 1 && _channelCount <= 16))return false;
if (!(_samplingRate >= 1 && _samplingRate <= 0x7FFFFF))return false;
}
else {
return false;
}
// comp
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706D6F63) {
stCompress *comp = (stCompress *)s; s += sizeof(stCompress);
_blockSize = bswap(comp->blockSize);
_comp_r01 = comp->r01;
_comp_r02 = comp->r02;
_comp_r03 = comp->r03;
_comp_r04 = comp->r04;
_comp_r05 = comp->r05;
_comp_r06 = comp->r06;
_comp_r07 = comp->r07;
_comp_r08 = comp->r08;
if (!((_blockSize >= 8 && _blockSize <= 0xFFFF) || (_blockSize == 0)))return false;
if (!(_comp_r01 >= 0 && _comp_r01 <= _comp_r02&&_comp_r02 <= 0x1F))return false;
}
// dec
else if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00636564) {
stDecode *dec = (stDecode *)s; s += sizeof(stDecode);
_blockSize = bswap(dec->blockSize);
_comp_r01 = dec->r01;
_comp_r02 = dec->r02;
_comp_r03 = dec->r04;
_comp_r04 = dec->r03;
_comp_r05 = dec->count1 + 1;
_comp_r06 = ((dec->enableCount2) ? dec->count2 : dec->count1) + 1;
_comp_r07 = _comp_r05 - _comp_r06;
_comp_r08 = 0;
if (!((_blockSize >= 8 && _blockSize <= 0xFFFF) || (_blockSize == 0)))return false;
if (!(_comp_r01 >= 0 && _comp_r01 <= _comp_r02&&_comp_r02 <= 0x1F))return false;
if (!_comp_r03)_comp_r03 = 1;
}
else {
return false;
}
// vbr
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00726276) {
stVBR *vbr = (stVBR *)s; s += sizeof(stVBR);
_vbr_r01 = bswap(vbr->r01);
_vbr_r02 = bswap(vbr->r02);
if (!(_blockSize == 0 && _vbr_r01 >= 0 && _vbr_r01 <= 0x1FF))return false;
}
else {
_vbr_r01 = 0;
_vbr_r02 = 0;
}
// ath
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x00687461) {
stATH *ath = (stATH *)s; s += 6;//s+=sizeof(stATH);
_ath_type = ath->type;
}
else {
_ath_type = (_version<0x200) ? 1 : 0;//v1.3ではデフォルト値が1になってたが、v2.0からATHテーブルが廃止されてるみたいなので0に
}
// loop
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x706F6F6C) {
stLoop *loop = (stLoop *)s; s += sizeof(stLoop);
_loopStart = bswap(loop->start);
_loopEnd = bswap(loop->end);
_loopCount = bswap(loop->count);
_loop_r01 = bswap(loop->r01);
_loopFlg = true;
if (!(_loopStart >= 0 && _loopStart <= _loopEnd&&_loopEnd<_blockCount))return false;
}
else {
_loopStart = 0;
_loopEnd = 0;
_loopCount = 0;
_loop_r01 = 0x400;
_loopFlg = false;
}
// ciph
if ((*(unsigned int *)s & 0x7F7F7F7F) == 0x68706963) {
stCipher *ciph = (stCipher *)s; s += 6;//s+=sizeof(stCipher);
_ciph_type = bswap(ciph->type);
if (!(_ciph_type == 0 || _ciph_type == 1 || _ciph_type == 0x38))return false;
}
else {
_ciph_type = 0;
}