forked from mlukasek/QDTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QDFFileReader.cs
377 lines (319 loc) · 14.8 KB
/
QDFFileReader.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using static QDTool.Utility;
namespace QDTool
{
class QDFFileReader
{
public long currentPosition = 0;
public long totalLength = 0;
public long bytesRemaining = 0;
public List<long> positions = new List<long>();
private static bool ValidateStartSign(byte[] startSign)
{
return startSign.SequenceEqual(ExpectedStartSign);
}
private static bool ValidateCrc(byte[] crc)
{
return crc.SequenceEqual(ExpectedCrc);
}
private static bool ValidateQDFHeader(MZQHeader header)
{
return ValidateStartSign(header.StartSign) && ValidateCrc(header.Crc);
}
private static bool ValidateQDiskMzfHeader(MZQFileHeader header)
{
return ValidateStartSign(header.StartSign) && ValidateCrc(header.Crc);
}
private static bool ValidateQDiskMzfBody(MZQFileBody body)
{
return ValidateStartSign(body.StartSign) && ValidateCrc(body.Crc);
}
public bool IsQDFHeader(BinaryReader reader)
{
byte[] expectedHeaderBytes = Encoding.ASCII.GetBytes("-QD format-")
.Concat(Enumerable.Repeat((byte)0xFF, 5)).ToArray();
byte[] headerBytes = reader.ReadBytes(expectedHeaderBytes.Length);
return headerBytes.SequenceEqual(expectedHeaderBytes);
}
public bool FindStartSequence(BinaryReader reader)
{
bool found00 = false; // Indikátor nalezení 0x00
int countOf16 = 0; // Počet postupných 0x16 bytů
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
byte currentByte = reader.ReadByte();
long currentPosition = reader.BaseStream.Position;
if (currentPosition == 0x12E8)
currentPosition++;
if (currentByte == 0x00)
{
found00 = true; // Nalezen 0x00, čekáme na 0x16
countOf16 = 0;
}
else // found00 je true
{
if (found00 && currentByte == 0x16)
{
countOf16++; // Počítání 0x16 bytů
}
else if (found00 && currentByte == 0xA5 && countOf16 >= 2)
{
return true; // Úspěšně nalezená sekvence
}
else
{
found00 = false; // Reset, pokud byte není 0x16 nebo 0xA5
}
}
}
return false;
}
public MZQHeader ReadQDFHeader(BinaryReader reader)
{
MZQHeader header = new MZQHeader();
header.StartSign = ZeroStartSign;
header.FileBlocksCount = 0;
header.Crc = ZeroCrc;
if (IsQDFHeader(reader))
{
if (FindStartSequence(reader))
{
positions.Add(reader.BaseStream.Position);
positions.Add(3);
header.StartSign = ExpectedStartSign;
CRC_check(0xA5, true);
header.FileBlocksCount = reader.ReadByte();
ushort crc = CRC_check(header.FileBlocksCount);
ushort readCRC = reader.ReadUInt16();
byte crcHi = ReverseBits((byte)(crc & 0xFF)); // reverse bits and swap hi-lo bytes too
byte crcLo = ReverseBits((byte)(crc >> 8));
ushort calculatedCRC = (ushort)(crcLo + (crcHi << 8));
//CRC_check((byte)(readCRC & 0xFF));
//CRC_check((byte)(readCRC >> 8));
crc = CRC_check(readCRC);
header.Crc = ExpectedCrc;
if (readCRC != calculatedCRC || crc != 0)
{
Array.Clear(header.StartSign, 0, header.StartSign.Length);
header.FileBlocksCount = 0;
Array.Clear(header.Crc, 0, header.Crc.Length);
}
}
}
return header;
}
public (MZQFileHeader, MZQFileBody) ReadQDFMzfBlock(BinaryReader reader)
{
MZQFileHeader header = new MZQFileHeader();
MZQFileBody body = new MZQFileBody();
if (FindStartSequence(reader))
{
positions.Add(reader.BaseStream.Position);
positions.Add(64+5);
// Načtení jednotlivých členů struktury
header.StartSign = ExpectedStartSign;
CRC_check(0xA5, true);
header.MzfHeaderSign = reader.ReadByte();
CRC_check(header.MzfHeaderSign);
header.DataSize = reader.ReadUInt16();
CRC_check(header.DataSize);
header.MzfFtype = reader.ReadByte();
CRC_check(header.MzfFtype);
header.MzfFname = reader.ReadBytes(16); // Předpokládáme, že MzfFname má vždy 16 bajtů
for (int i = 0; i < 16; i++)
{
CRC_check(header.MzfFname[i]);
}
header.MzfFnameEnd = reader.ReadByte();
CRC_check(header.MzfFnameEnd);
header.Unused1 = reader.ReadBytes(2); // Předpokládáme, že Unused1 má vždy 2 bajty
CRC_check(header.Unused1[0]);
CRC_check(header.Unused1[1]);
header.MzfSize = reader.ReadUInt16();
CRC_check(header.MzfSize);
header.MzfStart = reader.ReadUInt16();
CRC_check(header.MzfStart);
header.MzfExec = reader.ReadUInt16();
CRC_check(header.MzfExec);
// Načtení prvních 38 bajtů pro MzfHeaderDescription
header.MzfHeaderDescription = new byte[104]; // Inicializace pole 104 bajty
byte[] descriptionBytes = reader.ReadBytes(38); // Načtení pouze 38 bajtů
Array.Copy(descriptionBytes, header.MzfHeaderDescription, descriptionBytes.Length);
for (int i = 0; i < 38; i++)
{
CRC_check(header.MzfHeaderDescription[i]);
}
// Načtení CRC
header.Crc = ExpectedCrc;
ushort readCRC = reader.ReadUInt16();
ushort crc = CRC_check(readCRC);
if (crc != 0)
{
header.Crc = ZeroCrc;
throw new InvalidOperationException("Invalid CRC in QDF MZF Header");
}
if (!ValidateQDiskMzfHeader(header))
{
throw new InvalidOperationException("Neplatný MZF Header");
}
if (FindStartSequence(reader))
{
// Načtení začátku MZF Těla pro získání DataSize
// byte[] mzfBodyStartBytes = reader.ReadBytes(7); // Předpokládáme, že prvních 7 bajtů obsahuje StartSign (4 bajty), MzfBodySign (1 bajt) a DataSize (2 bajty)
// ushort bodyDataSize = // BitConverter.ToUInt16(mzfBodyStartBytes, 5);
positions.Add(reader.BaseStream.Position);
body.StartSign = ExpectedStartSign;
CRC_check(0xA5, true);
body.MzfBodySign = reader.ReadByte();
CRC_check(body.MzfBodySign);
body.DataSize = reader.ReadUInt16();
positions.Add(body.DataSize + 5);
CRC_check(body.DataSize);
body.MzfBody = reader.ReadBytes(body.DataSize);
for (int i = 0; i < body.DataSize; i++)
{
CRC_check(body.MzfBody[i]);
}
body.Crc = ExpectedCrc;
readCRC = reader.ReadUInt16();
crc = CRC_check(readCRC);
if (crc != 0)
{
body.Crc = ZeroCrc;
throw new InvalidOperationException("Invalid CRC in QDF MZF Body");
}
if (!ValidateQDiskMzfBody(body))
{
throw new InvalidOperationException("Invlid QD/MZF Header");
}
}
else
{
throw new InvalidOperationException("Missing data block after header block.");
}
}
return (header, body);
}
public List<(MZQFileHeader, MZQFileBody)> ReadFile(string filePath)
{
List<(MZQFileHeader, MZQFileBody)> mzfBlocks = new List<(MZQFileHeader, MZQFileBody)>();
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (BinaryReader reader = new BinaryReader(fs))
{
// Read the file header
MZQHeader header = ReadQDFHeader(reader);
if (!ValidateQDFHeader(header))
{
throw new InvalidOperationException("Neplatný QDFHeader");
}
//// Read each MZF block
while ((reader.BaseStream.Position < reader.BaseStream.Length) && (mzfBlocks.Count() < header.FileBlocksCount / 2))
{
var mzfBlock = ReadQDFMzfBlock(reader);
if(mzfBlock.Item1.MzfFname != null)
{
mzfBlocks.Add(mzfBlock);
}
}
currentPosition = fs.Position;
totalLength = fs.Length;
bytesRemaining = totalLength - currentPosition;
}
return mzfBlocks;
}
public void WriteBytesToStream(FileStream fileStream, byte val, long repeat)
{
for (long i = 0; i < repeat; i++)
{
fileStream.WriteByte(val);
}
}
public void WriteQDFHeaderToFile(FileStream fileStream, byte fbCount)
{
// BinaryWriter writer = new BinaryWriter(fileStream);
byte[] QDFFileSignature = Encoding.ASCII.GetBytes("-QD format-")
.Concat(Enumerable.Repeat((byte)0xFF, 5)).ToArray();
fileStream.Write(QDFFileSignature, 0, QDFFileSignature.Length);
WriteBytesToStream(fileStream, 0x00, 0x12EA - 16);
WriteBytesToStream(fileStream, 0x16, 9);
fileStream.WriteByte(0xA5);
CRC_check(0xA5, true);
fileStream.WriteByte(fbCount);
ushort crc = CRC_check(fbCount);
byte crcHi = ReverseBits((byte)(crc & 0xFF)); // reverse bits and swap hi-lo bytes too
byte crcLo = ReverseBits((byte)(crc >> 8));
ushort calculatedCRC = (ushort)(crcLo + (crcHi << 8));
fileStream.WriteByte(crcLo);
fileStream.WriteByte(crcHi);
WriteBytesToStream(fileStream, 0x16, 6);
WriteBytesToStream(fileStream, 0x00, 2794);
//header.Crc = Encoding.ASCII.GetBytes("CRC");
//fileStream.Write(header.Crc, 0, header.Crc.Length);
}
public void WriteQDFFileHeaderToFile(FileStream fileStream, MZQFileHeader mzfHeader)
{
WriteBytesToStream(fileStream, 0x16, 10);
fileStream.WriteByte(0xA5);
CRC_check(0xA5, true);
fileStream.WriteByte(mzfHeader.MzfHeaderSign);
CRC_check(mzfHeader.MzfHeaderSign);
var dataSizeBytes = BitConverter.GetBytes(mzfHeader.DataSize);
fileStream.Write(dataSizeBytes, 0, dataSizeBytes.Length);
CRC_check(dataSizeBytes, 0, dataSizeBytes.Length);
fileStream.WriteByte(mzfHeader.MzfFtype);
CRC_check(mzfHeader.MzfFtype);
fileStream.Write(mzfHeader.MzfFname, 0, mzfHeader.MzfFname.Length);
CRC_check(mzfHeader.MzfFname, 0, mzfHeader.MzfFname.Length);
fileStream.WriteByte(mzfHeader.MzfFnameEnd);
CRC_check(mzfHeader.MzfFnameEnd);
fileStream.Write(mzfHeader.Unused1, 0, mzfHeader.Unused1.Length);
CRC_check(mzfHeader.Unused1, 0, mzfHeader.Unused1.Length);
var mzfSizeBytes = BitConverter.GetBytes(mzfHeader.MzfSize);
fileStream.Write(mzfSizeBytes, 0, mzfSizeBytes.Length);
CRC_check(mzfSizeBytes, 0, mzfSizeBytes.Length);
var mzfStartBytes = BitConverter.GetBytes(mzfHeader.MzfStart);
fileStream.Write(mzfStartBytes, 0, mzfStartBytes.Length);
CRC_check(mzfStartBytes, 0, mzfStartBytes.Length);
var mzfExecBytes = BitConverter.GetBytes(mzfHeader.MzfExec);
fileStream.Write(mzfExecBytes, 0, mzfExecBytes.Length);
CRC_check(mzfExecBytes, 0, mzfExecBytes.Length);
fileStream.Write(mzfHeader.MzfHeaderDescription, 0, 38); // mzfHeader.MzfHeaderDescription.Length
ushort crc = CRC_check(mzfHeader.MzfHeaderDescription, 0, 38);
byte crcHi = ReverseBits((byte)(crc & 0xFF)); // reverse bits and swap hi-lo bytes too
byte crcLo = ReverseBits((byte)(crc >> 8));
ushort calculatedCRC = (ushort)(crcLo + (crcHi << 8));
fileStream.WriteByte(crcLo);
fileStream.WriteByte(crcHi);
WriteBytesToStream(fileStream, 0x16, 7);
WriteBytesToStream(fileStream, 0x00, 254);
}
public void WriteQDFFileBodyToFile(FileStream fileStream, MZQFileBody mzfBody)
{
WriteBytesToStream(fileStream, 0x16, 10);
fileStream.WriteByte(0xA5);
CRC_check(0xA5, true);
fileStream.WriteByte(mzfBody.MzfBodySign);
CRC_check(mzfBody.MzfBodySign);
var dataSizeBytes = BitConverter.GetBytes(mzfBody.DataSize);
fileStream.Write(dataSizeBytes, 0, dataSizeBytes.Length);
CRC_check(dataSizeBytes, 0, dataSizeBytes.Length);
fileStream.Write(mzfBody.MzfBody, 0, mzfBody.DataSize);
ushort crc = CRC_check(mzfBody.MzfBody, 0, mzfBody.DataSize);
byte crcHi = ReverseBits((byte)(crc & 0xFF)); // reverse bits and swap hi-lo bytes too
byte crcLo = ReverseBits((byte)(crc >> 8));
ushort calculatedCRC = (ushort)(crcLo + (crcHi << 8));
fileStream.WriteByte(crcLo);
fileStream.WriteByte(crcHi);
WriteBytesToStream(fileStream, 0x16, 7);
WriteBytesToStream(fileStream, 0x00, 256);
}
}
}