forked from robcfg/retrotools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdskfile.cpp
More file actions
383 lines (323 loc) · 10.7 KB
/
dskfile.cpp
File metadata and controls
383 lines (323 loc) · 10.7 KB
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
///////////////////////////////////////////////////////////////////
//
// dskfile.cpp - Implementation of the CDSKFile class.
// It's purpose it's to read and write
// disk image files in the format defined
// by Marco Vieth, Ulrich Doewich and Kevin Thacker.
//
// Created 10/02/2017 by Roberto Carlos Fernández Gerhardt.
//
// Last update on 12/02/2017.
//
///////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <cstring>
#include "dskfile.h"
// debug includes ////////
#include <iostream>
//////////////////////////
//////////////////////////////////////////
// Default constructor
//////////////////////////////////////////
CDSKFile::CDSKFile()
{
lastError = "No error.";
isExtendedDSK = false;
}
//////////////////////////////////////////
// Destructor
//////////////////////////////////////////
CDSKFile::~CDSKFile()
{
}
//////////////////////////////////////////
// Load - Read a DSK file and store its
// data in the adequate structures.
//
// Returns true if load was successful,
// false otherwise.
// In case of error, the cause will be
// stored in the lastError string.
//////////////////////////////////////////
bool CDSKFile::Load(const string& _filename)
{
// Open File
FILE* pIn = fopen(_filename.c_str(),"rb");
if( NULL == pIn )
{
lastError = "Could not open file ";
lastError += _filename;
lastError += " for reading.";
return false;
}
// Check file size. Must be at least 256 bytes long to be able to read the header
fseek(pIn,0,SEEK_END);
size_t fileSize = ftell(pIn);
fseek(pIn,0,SEEK_SET);
if( fileSize < 256 )
{
lastError = "File ";
lastError += _filename;
lastError += " is less than 256 bytes long. It's either corrupted or not a .DSK file.";
return false;
}
// Clear data. Any other info from a previous load will be overwritten.
diskInfoBlock.trackSizeTable.clear();
sides[0].clear();
sides[1].clear();
// Read DISK INFORMATION BLOCK
ReadDiskInformationBlock(pIn);
// Read tracks
for( unsigned short int track = 0; track < diskInfoBlock.tracksNum; ++track )
{
for( unsigned char side = 0; side < diskInfoBlock.sidesNum; ++side )
{
if(diskInfoBlock.trackSize || diskInfoBlock.trackSizeTable[ (track * diskInfoBlock.sidesNum) + side] )
{
ReadTrackInformationBlock(pIn);
}
else
{
// Insert unformatted track info
CDSKFile_TrackInfoBlock tib;
tib.isUnformatted = true;
sides[side].push_back(tib);
}
}
}
// Close file
fclose(pIn);
return true;
}
//////////////////////////////////////////
// Save - Write current data to a DSK
// file.
//
// Returns true if load was successful,
// false otherwise.
// In case of error, the cause will be
// stored in the lastError string.
//////////////////////////////////////////
bool CDSKFile::Save(const string& _filename)
{
// Open File
FILE* pOut = fopen(_filename.c_str(),"wb");
if( NULL == pOut )
{
lastError = "Could not open file ";
lastError += _filename;
lastError += " for writing.";
return false;
}
// Write DISK INFORMATION BLOCK
WriteDiskInformationBlock(pOut);
// Write tracks
for( unsigned short int track = 0; track < diskInfoBlock.tracksNum; ++track )
{
for( unsigned char side = 0; side < diskInfoBlock.sidesNum; ++side )
{
WriteTrackInformationBlock(pOut,sides[side][track]);
}
}
// Close File
fclose(pOut);
return true;
}
///////////////////////////////////////////////////////////////////////
// ReadDiskInformationBlock - Read a CDSKFile_DiskInfoBlock structure
// from an open file.
///////////////////////////////////////////////////////////////////////
void CDSKFile::ReadDiskInformationBlock(FILE* _pIn)
{
char tmpBuf[35];
// Read header string
memset(tmpBuf,0,35);
fread(tmpBuf,1,34,_pIn);
diskInfoBlock.header = tmpBuf;
isExtendedDSK = (diskInfoBlock.header.substr(0,8) == "EXTENDED");
// Read creator string
memset(tmpBuf,0,35);
fread(tmpBuf,1,14,_pIn);
diskInfoBlock.creator = tmpBuf;
// Read track number and size, and side number
fread(&diskInfoBlock.tracksNum,1,1,_pIn);
fread(&diskInfoBlock.sidesNum ,1,1,_pIn);
fread(&diskInfoBlock.trackSize,1,2,_pIn);
// Read track size table
unsigned int tableLength = diskInfoBlock.tracksNum * diskInfoBlock.sidesNum;
for( unsigned int tableByte = 0; tableByte < tableLength; ++tableByte )
{
fread(&tmpBuf[0],1,1,_pIn);
diskInfoBlock.trackSizeTable.push_back(tmpBuf[0]);
}
// Skip unused block data
fseek( _pIn, 256, SEEK_SET );
}
///////////////////////////////////////////////////////////////////////
// WriteDiskInformationBlock - Write a CDSKFile_DiskInfoBlock structure
// to an open file.
///////////////////////////////////////////////////////////////////////
void CDSKFile::WriteDiskInformationBlock(FILE* _pOut) const
{
size_t written = 0;
char tmpBuf[35];
// Write header string
written += fwrite(diskInfoBlock.header.c_str(),1,34,_pOut);
// Write creator string
written += fwrite(diskInfoBlock.creator.c_str(),1,14,_pOut);
// Write track number and size, and side number
written += fwrite(&diskInfoBlock.tracksNum,1,1,_pOut);
written += fwrite(&diskInfoBlock.sidesNum ,1,1,_pOut);
written += fwrite(&diskInfoBlock.trackSize,1,2,_pOut);
// Write track size table
written += fwrite(diskInfoBlock.trackSizeTable.data(),1,diskInfoBlock.trackSizeTable.size(),_pOut);
// Add padding bytes
tmpBuf[0] = 0;
size_t padding = 256 - written;
for( size_t paddingByte = 0; paddingByte < padding; ++paddingByte )
{
fwrite(&tmpBuf[0],1,1,_pOut);
}
}
///////////////////////////////////////////////////////////////////////
// ReadTrackInformationBlock - Read a CDSKFile_TrackInfoBlock structure
// and associated sector info and data
// from an open file.
///////////////////////////////////////////////////////////////////////
void CDSKFile::ReadTrackInformationBlock(FILE* _pIn)
{
CDSKFile_TrackInfoBlock tib;
tib.isUnformatted = false;
char tmpBuf[14];
// Store current file offset to skip unused data after reading block.
// The Track info block is 256 bytes long on disk.
size_t fileOffset = ftell(_pIn);
// Read header
memset(tmpBuf,0,14);
fread(tmpBuf,1,13,_pIn);
tib.header = tmpBuf;
// Read data
fread( tib.unused ,1,3,_pIn);
fread(&tib.trackNumber ,1,1,_pIn);
fread(&tib.sideNumber ,1,1,_pIn);
fread(&tib.dataRate ,1,1,_pIn);
fread(&tib.recordingMode,1,1,_pIn);
fread(&tib.sectorSize ,1,1,_pIn);
fread(&tib.sectorsNum ,1,1,_pIn);
fread(&tib.gap3Length ,1,1,_pIn);
fread(&tib.fillerByte ,1,1,_pIn);
// Read sector info blocks
for( unsigned char sector = 0; sector < tib.sectorsNum; ++sector )
{
ReadSectorInformationBlock(_pIn,tib);
}
// Skip unused data
fseek( _pIn, fileOffset+256, SEEK_SET );
// Read actual sector data
for( unsigned char sector = 0; sector < tib.sectorsNum; ++sector )
{
unsigned short int dataSize = tib.sectorInfoList[sector].dataLength;
if( 0 == dataSize )
{
if( 0 != tib.sectorSize )
{
dataSize = (256 << (tib.sectorSize - 1));
}
else
{
return;
}
}
uint8vector tmpVec;
for( unsigned short int dataByte = 0; dataByte < dataSize; ++dataByte )
{
fread(&tmpBuf[0],1,1,_pIn);
tmpVec.push_back(tmpBuf[0]);
}
tib.sectorData.push_back(tmpVec);
}
// Insert track data
sides[tib.sideNumber].push_back(tib);
}
/////////////////////////////////////////////////////////////////////////
// WriteTrackInformationBlock - Write a CDSKFile_TrackInfoBlock structure
// and associated sector info and data
// to an open file.
/////////////////////////////////////////////////////////////////////////
void CDSKFile::WriteTrackInformationBlock(FILE* _pOut, const CDSKFile_TrackInfoBlock& _tib) const
{
// Write only formatted tracks
if( _tib.isUnformatted )
return;
size_t written = 0;
char tmpBuf[35];
// Write header
written += fwrite(_tib.header.c_str(),1,13,_pOut);
// Write data
written += fwrite( _tib.unused ,1,3,_pOut);
written += fwrite(&_tib.trackNumber ,1,1,_pOut);
written += fwrite(&_tib.sideNumber ,1,1,_pOut);
written += fwrite(&_tib.dataRate ,1,1,_pOut);
written += fwrite(&_tib.recordingMode,1,1,_pOut);
written += fwrite(&_tib.sectorSize ,1,1,_pOut);
written += fwrite(&_tib.sectorsNum ,1,1,_pOut);
written += fwrite(&_tib.gap3Length ,1,1,_pOut);
written += fwrite(&_tib.fillerByte ,1,1,_pOut);
// Write sector info blocks
for( unsigned char sector = 0; sector < _tib.sectorsNum; ++sector )
{
written += WriteSectorInformationBlock(_pOut,_tib.sectorInfoList[sector]);
}
// Add padding bytes
tmpBuf[0] = 0;
size_t padding = 256 - written;
for( size_t paddingByte = 0; paddingByte < padding; ++paddingByte )
{
fwrite(&tmpBuf[0],1,1,_pOut);
}
// Write sector data
for( unsigned char sector = 0; sector < _tib.sectorsNum; ++sector )
{
unsigned short int dataSize = _tib.sectorInfoList[sector].dataLength;
fwrite(_tib.sectorData[sector].data(),1,dataSize,_pOut);
}
}
////////////////////////////////////////////////////////////////////////
// ReadSectorInformationBlock - Read a CDSKFile_SectorInfo structure
// from an open file and store it in the
// given CDSKFile_TrackInfoBlock structure.
////////////////////////////////////////////////////////////////////////
void CDSKFile::ReadSectorInformationBlock(FILE* _pIn, CDSKFile_TrackInfoBlock& _tib)
{
CDSKFile_SectorInfo si;
fread(&si.track ,1,1,_pIn);
fread(&si.side ,1,1,_pIn);
fread(&si.sectorID ,1,1,_pIn);
fread(&si.sectorSize,1,1,_pIn);
fread(&si.FDCStatus1,1,1,_pIn);
fread(&si.FDCStatus2,1,1,_pIn);
fread(&si.dataLength,1,2,_pIn);
_tib.sectorInfoList.push_back(si);
}
////////////////////////////////////////////////////////////////////////
// WriteSectorInformationBlock - Write a CDSKFile_SectorInfo structure
// to an open file.
//
// Returns the number of bytes written
// to the file to allow calculation of
// the number of padding bytes in the
// WriteTrackInformationBlock function.
////////////////////////////////////////////////////////////////////////
size_t CDSKFile::WriteSectorInformationBlock(FILE* _pOut, const CDSKFile_SectorInfo& _si) const
{
size_t written = 0;
written += fwrite(&_si.track ,1,1,_pOut);
written += fwrite(&_si.side ,1,1,_pOut);
written += fwrite(&_si.sectorID ,1,1,_pOut);
written += fwrite(&_si.sectorSize,1,1,_pOut);
written += fwrite(&_si.FDCStatus1,1,1,_pOut);
written += fwrite(&_si.FDCStatus2,1,1,_pOut);
written += fwrite(&_si.dataLength,1,2,_pOut);
return written;
}