-
Notifications
You must be signed in to change notification settings - Fork 9
/
ECCEDC.cpp
257 lines (213 loc) · 9.81 KB
/
ECCEDC.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
// CheckSector.cpp: CCheckSector クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CheckSector.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////
typedef struct tMode1Frame{
BYTE Sync[12];
BYTE Min;
BYTE Sec;
BYTE Frame;
BYTE Mode;
BYTE Data[2048];
DWORD Edc;
BYTE Zero[8];
BYTE ECC_P[172];
BYTE ECC_Q[104];
} Mode1Frame;
static const BYTE EDC_Sync[12] = {
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
static const DWORD EDC_crctable[256] = {
0x00000000L, 0x90910101L, 0x91210201L, 0x01B00300L, 0x92410401L, 0x02D00500L, 0x03600600L, 0x93F10701L,
0x94810801L, 0x04100900L, 0x05A00A00L, 0x95310B01L, 0x06C00C00L, 0x96510D01L, 0x97E10E01L, 0x07700F00L,
0x99011001L, 0x09901100L, 0x08201200L, 0x98B11301L, 0x0B401400L, 0x9BD11501L, 0x9A611601L, 0x0AF01700L,
0x0D801800L, 0x9D111901L, 0x9CA11A01L, 0x0C301B00L, 0x9FC11C01L, 0x0F501D00L, 0x0EE01E00L, 0x9E711F01L,
0x82012001L, 0x12902100L, 0x13202200L, 0x83B12301L, 0x10402400L, 0x80D12501L, 0x81612601L, 0x11F02700L,
0x16802800L, 0x86112901L, 0x87A12A01L, 0x17302B00L, 0x84C12C01L, 0x14502D00L, 0x15E02E00L, 0x85712F01L,
0x1B003000L, 0x8B913101L, 0x8A213201L, 0x1AB03300L, 0x89413401L, 0x19D03500L, 0x18603600L, 0x88F13701L,
0x8F813801L, 0x1F103900L, 0x1EA03A00L, 0x8E313B01L, 0x1DC03C00L, 0x8D513D01L, 0x8CE13E01L, 0x1C703F00L,
0xB4014001L, 0x24904100L, 0x25204200L, 0xB5B14301L, 0x26404400L, 0xB6D14501L, 0xB7614601L, 0x27F04700L,
0x20804800L, 0xB0114901L, 0xB1A14A01L, 0x21304B00L, 0xB2C14C01L, 0x22504D00L, 0x23E04E00L, 0xB3714F01L,
0x2D005000L, 0xBD915101L, 0xBC215201L, 0x2CB05300L, 0xBF415401L, 0x2FD05500L, 0x2E605600L, 0xBEF15701L,
0xB9815801L, 0x29105900L, 0x28A05A00L, 0xB8315B01L, 0x2BC05C00L, 0xBB515D01L, 0xBAE15E01L, 0x2A705F00L,
0x36006000L, 0xA6916101L, 0xA7216201L, 0x37B06300L, 0xA4416401L, 0x34D06500L, 0x35606600L, 0xA5F16701L,
0xA2816801L, 0x32106900L, 0x33A06A00L, 0xA3316B01L, 0x30C06C00L, 0xA0516D01L, 0xA1E16E01L, 0x31706F00L,
0xAF017001L, 0x3F907100L, 0x3E207200L, 0xAEB17301L, 0x3D407400L, 0xADD17501L, 0xAC617601L, 0x3CF07700L,
0x3B807800L, 0xAB117901L, 0xAAA17A01L, 0x3A307B00L, 0xA9C17C01L, 0x39507D00L, 0x38E07E00L, 0xA8717F01L,
0xD8018001L, 0x48908100L, 0x49208200L, 0xD9B18301L, 0x4A408400L, 0xDAD18501L, 0xDB618601L, 0x4BF08700L,
0x4C808800L, 0xDC118901L, 0xDDA18A01L, 0x4D308B00L, 0xDEC18C01L, 0x4E508D00L, 0x4FE08E00L, 0xDF718F01L,
0x41009000L, 0xD1919101L, 0xD0219201L, 0x40B09300L, 0xD3419401L, 0x43D09500L, 0x42609600L, 0xD2F19701L,
0xD5819801L, 0x45109900L, 0x44A09A00L, 0xD4319B01L, 0x47C09C00L, 0xD7519D01L, 0xD6E19E01L, 0x46709F00L,
0x5A00A000L, 0xCA91A101L, 0xCB21A201L, 0x5BB0A300L, 0xC841A401L, 0x58D0A500L, 0x5960A600L, 0xC9F1A701L,
0xCE81A801L, 0x5E10A900L, 0x5FA0AA00L, 0xCF31AB01L, 0x5CC0AC00L, 0xCC51AD01L, 0xCDE1AE01L, 0x5D70AF00L,
0xC301B001L, 0x5390B100L, 0x5220B200L, 0xC2B1B301L, 0x5140B400L, 0xC1D1B501L, 0xC061B601L, 0x50F0B700L,
0x5780B800L, 0xC711B901L, 0xC6A1BA01L, 0x5630BB00L, 0xC5C1BC01L, 0x5550BD00L, 0x54E0BE00L, 0xC471BF01L,
0x6C00C000L, 0xFC91C101L, 0xFD21C201L, 0x6DB0C300L, 0xFE41C401L, 0x6ED0C500L, 0x6F60C600L, 0xFFF1C701L,
0xF881C801L, 0x6810C900L, 0x69A0CA00L, 0xF931CB01L, 0x6AC0CC00L, 0xFA51CD01L, 0xFBE1CE01L, 0x6B70CF00L,
0xF501D001L, 0x6590D100L, 0x6420D200L, 0xF4B1D301L, 0x6740D400L, 0xF7D1D501L, 0xF661D601L, 0x66F0D700L,
0x6180D800L, 0xF111D901L, 0xF0A1DA01L, 0x6030DB00L, 0xF3C1DC01L, 0x6350DD00L, 0x62E0DE00L, 0xF271DF01L,
0xEE01E001L, 0x7E90E100L, 0x7F20E200L, 0xEFB1E301L, 0x7C40E400L, 0xECD1E501L, 0xED61E601L, 0x7DF0E700L,
0x7A80E800L, 0xEA11E901L, 0xEBA1EA01L, 0x7B30EB00L, 0xE8C1EC01L, 0x7850ED00L, 0x79E0EE00L, 0xE971EF01L,
0x7700F000L, 0xE791F101L, 0xE621F201L, 0x76B0F300L, 0xE541F401L, 0x75D0F500L, 0x7460F600L, 0xE4F1F701L,
0xE381F801L, 0x7310F900L, 0x72A0FA00L, 0xE231FB01L, 0x71C0FC00L, 0xE151FD01L, 0xE0E1FE01L, 0x7070FF00L};
static const unsigned char rs_l12_alog[255] = {
1, 2, 4, 8,16,32,64,128,29,58,116,232,205,135,19,38,76,152,45,90,180,117,234,201,143, 3, 6,12,24,48,96,192,157,39,78,156,37,74,148,53,106,212,181,119,238,193,159,35,70,140, 5,10,20,40,80,160,93,186,105,210,185,111,222,161,95,190,97,194,153,47,94,188,101,202,137,15,30,60,120,240,253,231,211,187,107,214,177,127,254,225,223,163,91,182,113,226,217,175,67,134,17,34,68,136,13,26,52,104,208,189,103,206,129,31,62,124,248,237,199,147,59,118,236,197,151,51,102,204,133,23,46,92,184,109,218,169,79,158,33,66,132,21,42,84,168,77,154,41,82,164,85,170,73,146,57,114,228,213,183,115,230,209,191,99,198,145,63,126,252,229,215,179,123,246,241,255,227,219,171,75,150,49,98,196,149,55,110,220,165,87,174,65,130,25,50,100,200,141, 7,14,28,56,112,224,221,167,83,166,81,162,89,178,121,242,249,239,195,155,43,86,172,69,138, 9,18,36,72,144,61,122,244,245,247,243,251,235,203,139,11,22,44,88,176,125,250,233,207,131,27,54,108,216,173,71,142};
static const unsigned char rs_l12_log[256] = {
0, 0, 1,25, 2,50,26,198, 3,223,51,238,27,104,199,75, 4,100,224,14,52,141,239,129,28,193,105,248,200, 8,76,113, 5,138,101,47,225,36,15,33,53,147,142,218,240,18,130,69,29,181,194,125,106,39,249,185,201,154, 9,120,77,228,114,166, 6,191,139,98,102,221,48,253,226,152,37,179,16,145,34,136,54,208,148,206,143,150,219,189,241,210,19,92,131,56,70,64,30,66,182,163,195,72,126,110,107,58,40,84,250,133,186,61,202,94,155,159,10,21,121,43,78,212,229,172,115,243,167,87, 7,112,192,247,140,128,99,13,103,74,222,237,49,197,254,24,227,165,153,119,38,184,180,124,17,68,146,217,35,32,137,46,55,63,209,91,149,188,207,205,144,135,151,178,220,252,190,97,242,86,211,171,20,42,93,158,132,60,57,83,71,109,65,162,31,45,67,216,183,123,164,118,196,23,73,236,127,12,111,246,108,161,59,82,41,157,85,170,251,96,134,177,187,204,62,90,203,89,95,176,156,169,160,81,11,245,22,235,122,117,44,215,79,174,213,233,230,231,173,232,116,214,244,234,168,80,88,175};
static const unsigned char DQ[2][43] = {
{190,96,250,132,59,81,159,154,200,7,111,245,10,20,41,156,168,79,173,231,229,171,210,240,17,67,215,43,120,8,199,74,102,220,251,95,175,87,166,113,75,198,25},
{97,251,133,60,82,160,155,201,8,112,246,11,21,42,157,169,80,174,232,230,172,211,241,18,68,216,44,121,9,200,75,103,221,252,96,176,88,167,114,76,199,26,1,}};
static const unsigned char DP[2][24] = {
{231,229,171,210,240,17,67,215,43,120,8,199,74,102,220,251,95,175,87,166,113,75,198,25},
{230,172,211,241,18,68,216,44,121,9,200,75,103,221,252,96,176,88,167,114,76,199,26,1,}};
extern const char *efm_table[256];
#define EFM(a,b) *(m_EFMTable+a*0x10+b)
CCheckSector::CCheckSector()
{
}
CCheckSector::~CCheckSector()
{
}
int CCheckSector::CheckMode1Raw(LPCSTR raw, int M, int S, int F)
{
BYTE rM, rS, rF;
Mode1Frame *frame;
frame = (Mode1Frame*)raw;
rM = ((M / 10) * 0x10) + (M % 10);
rS = ((S / 10) * 0x10) + (S % 10);
rF = ((F / 10) * 0x10) + (F % 10);
if(MemCmp(raw,(LPCSTR)EDC_Sync,12)){
return SECTOR_BADSYNC;
}
if(frame->Min != rM || frame->Sec != rS || frame->Frame != rF){
m_OriginalM = ((frame->Min / 16) * 10) + (frame->Min % 16);
m_OriginalS = ((frame->Sec / 16) * 10) + (frame->Sec % 16);
m_OriginalF = ((frame->Frame / 16) * 10) + (frame->Frame % 16);
return SECTOR_BADMSF;
}
m_CalcEDC = CalcEDC(raw);
if(frame->Edc != m_CalcEDC){
m_OriginalEDC = frame->Edc;
return SECTOR_BADEDC;
}
CalcEccP((BYTE*)raw+12);
if(MemCmp(raw+2076,(char*)m_EccP,172)){
return SECTOR_BADECC;
}
CalcEccQ((BYTE*)raw+12);
if(MemCmp(raw+2248,(char*)m_EccQ,104)){
return SECTOR_BADECC;
}
return SECTOR_OK;
}
int CCheckSector::MemCmp(LPCSTR Mem1, LPCSTR Mem2, DWORD Count)
{
DWORD i;
for(i=0;i<Count;i++){
if(*Mem1 != *Mem2){
return *Mem1 - *Mem2;
}
Mem1++;
Mem2++;
}
return 0;
}
DWORD CCheckSector::CalcEDC(LPCSTR Buffer)
{
LPCSTR p = Buffer;
DWORD result = 0;
int i;
for (i=0;i<2064;i++){
result = EDC_crctable[(result ^ *p++) & 0xff] ^ (result >> 8);
}
return result;
}
void CCheckSector::CalcEccQ(unsigned char *buffer)
{
unsigned char *Q;
int i,j;
Q = m_EccQ;
memset(Q, 0, 104);
for (j = 0; j < 26; j++) {
for (i = 0; i < 43; i++) {
unsigned char data;
/* LSB */
data = buffer[(j*86+i*88) % (2236)];
if (data != 0) {
unsigned int base = rs_l12_log[data];
unsigned int sum = base + DQ[0][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
Q[0] ^= rs_l12_alog[sum];
sum = base + DQ[1][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
Q[26*2] ^= rs_l12_alog[sum];
}
/* MSB */
data = buffer[(j*86+i*88+1) % (4 + 2048 + 4 + 8 + 172)];
if (data != 0) {
unsigned int base = rs_l12_log[data];
unsigned int sum = base+DQ[0][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
Q[1] ^= rs_l12_alog[sum];
sum = base + DQ[1][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
Q[26*2+1] ^= rs_l12_alog[sum];
}
}
Q += 2;
}
return;
}
void CCheckSector::CalcEccP(unsigned char *buffer)
{
unsigned char *P;
int i,j;
P = m_EccP;
memset(P, 0, 172);
for (j = 0; j < 43; j++) {
for (i = 0; i < 24; i++) {
unsigned char data;
/* LSB */
data = buffer[i*86];
if (data != 0) {
unsigned int base = rs_l12_log[data];
unsigned int sum = base + DP[0][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
P[0] ^= rs_l12_alog[sum];
sum = base + DP[1][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
P[43*2] ^= rs_l12_alog[sum];
}
/* MSB */
data = buffer[i*86+1];
if (data != 0) {
unsigned int base = rs_l12_log[data];
unsigned int sum = base + DP[0][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
P[1] ^= rs_l12_alog[sum];
sum = base + DP[1][i];
if (sum >= ((1 << 8)-1))
sum -= (1 << 8)-1;
P[43*2+1] ^= rs_l12_alog[sum];
}
}
P += 2;
buffer += 2;
}
return;
}