-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskimagepro.cpp
executable file
·165 lines (144 loc) · 4.78 KB
/
diskimagepro.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
#include "diskimagepro.h"
#include <QtDebug>
DiskImagePro::~DiskImagePro()
{
close();
}
void DiskImagePro::close()
{
}
bool DiskImagePro::format(quint16, quint16)
{
return false;
}
bool DiskImagePro::open(const QString &fileName, FileTypes::FileType /* type */)
{
if (m_originalImageType == FileTypes::Atr) {
sourceFile = new QFile(fileName);
} else {
sourceFile = new GzFile(fileName);
}
// Try to open the source file
if (!sourceFile->open(QFile::Unbuffered | QFile::ReadOnly)) {
qCritical() << "!e" << tr("Cannot open '%1': %2").arg(fileName).arg(sourceFile->errorString());
delete sourceFile;
return false;
}
// Try to read the header
QByteArray header;
header = sourceFile->read(16);
if (header.size() != 16) {
qCritical() << "!e" << tr("Cannot open '%1': %2")
.arg(fileName)
.arg(tr("Cannot read the header: %1.").arg(sourceFile->errorString()));
delete sourceFile;
return false;
}
// Validate the magic number
quint16 magic = ((quint8)header[0]) * 256 + ((quint8)header[1]);
if (magic != ((sourceFile->size()-16)/(128+12)) && header[2] == 'P') {
qCritical() << "!e" << tr("Cannot open '%1': %2").arg(fileName).arg(tr("Not a valid PRO file."));
delete sourceFile;
return false;
}
// Validate disk geometry
DiskGeometry geometry;
geometry.initialize(0, 40, (sourceFile->size() >= 1040*(128+12)+16 ? 26 : 18), 128);
if (geometry.sectorCount() > 65535) {
qCritical() << "!e" << tr("Cannot open '%1': %2")
.arg(fileName)
.arg(tr("Too many sectors in the image (%1).").arg(geometry.sectorCount()));
file.close();
delete sourceFile;
return false;
}
// Update the image information
m_geometry.initialize(geometry);
refreshNewGeometry();
m_isReadOnly = true;
m_originalFileName = fileName;
m_originalFileHeader = header;
m_isModified = false;
m_isUnmodifiable = true;
m_isUnnamed = false;
return true;
}
bool DiskImagePro::seekToSector(quint16 sector)
{
if (sector < 1 || sector > m_geometry.sectorCount()) {
qCritical() << "!e" << tr("[%1] Cannot seek to sector %2: %3")
.arg(deviceName())
.arg(sector)
.arg(tr("Sector number is out of bounds."));
}
qint64 pos = 16 + (sector - 1) * (m_geometry.bytesPerSector()+12);
if (m_geometry.bytesPerSector() == 256) {
if (sector <= 3) {
pos = (sector - 1) * (128+12);
} else {
pos -= (384+36);
}
}
if (!sourceFile->seek(pos)) {
qCritical() << "!e" << tr("[%1] Cannot seek to sector %2: %3")
.arg(deviceName())
.arg(sector)
.arg(sourceFile->error());
return false;
}
return true;
}
bool DiskImagePro::readSector(quint16 sector, QByteArray &data)
{
if (!seekToSector(sector)) {
return false;
}
QByteArray header;
header = sourceFile->read(12);
if ((quint8)header[5] != 0)
{
int dupnum = count[sector];
qDebug() << "!e" << tr("Duplicate sector: %1 dupnum: %2").arg(sector).arg(dupnum);
count[sector] = (count[sector]+1) % ((quint8)header[5]+1);
if (dupnum != 0) {
sector = m_geometry.sectorCount() + (quint8)header[6+dupnum];
/* can dupnum be 5? */
if (dupnum > 4 || sector <= 0 || sector > ((sourceFile->size()-16)/(128+12)))
{
qCritical() << "!e" << tr("Error in .pro image: sector: %1 dupnum: %2").arg(sector).arg(dupnum);
return false;
}
seekToSector(sector);
/* read sector header */
header = sourceFile->read(12);
}
}
wd1772status = header[1];
if (wd1772status != 0xff)
{
qDebug() << "!e" << tr("Bad sector");
return false;
}
data = sourceFile->read(m_geometry.bytesPerSector(sector));
if (data.size() != m_geometry.bytesPerSector(sector)) {
qCritical() << "!e" << tr("[%1] Cannot read from sector %2: %3.")
.arg(deviceName())
.arg(sector)
.arg(sourceFile->errorString());
return false;
}
return true;
}
bool DiskImagePro::writeSector(quint16, const QByteArray &)
{
return false;
}
void DiskImagePro::getStatus(QByteArray &status)
{
status[0] = m_isReadOnly * 8 |
(m_newGeometry.bytesPerSector() == 256) * 32 |
(m_newGeometry.bytesPerSector() == 128 && m_newGeometry.sectorsPerTrack() == 26) * 128;
status[1] = wd1772status;
status[2] = 3;
status[3] = 0;
}