forked from kgober/FSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageDisk.cs
163 lines (154 loc) · 6.06 KB
/
ImageDisk.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
// ImageDisk.cs
// Copyright © 2019-2020 Kenneth Gober
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Future Improvements / To Do
// use better heuristic to guess default sector size
// support cylinder/head mapping
// support .IMD file writing
using System;
using System.Text;
namespace FSX
{
class ImageDisk
{
public static Boolean HasHeader(Byte[] data)
{
if (data.Length < 32) return false;
if (Buffer.IndexOf(data, 0, "IMD ", Encoding.ASCII) != 0) return false;
return true;
}
// Load ImageDisk .IMD image file
public static CHSVolume Load(String source, Byte[] data)
{
Int32 p, n;
Int32[] SS = new Int32[0];
// determine disk geometry
Int32 nc = -1;
Int32 nh = -1;
Int32 ss = -1;
for (p = 0; data[p++] != 0x1a; ) ; // skip ASCII header
while (p < data.Length)
{
// track header
p++; // skip mode
Int32 c = data[p++]; // cylinder num
if (c > nc) nc = c;
Int32 h = data[p++]; // head num
Boolean cm = ((h & 0x80) != 0);
Boolean hm = ((h & 0x40) != 0);
h &= 0x3f;
if (h > nh) nh = h;
Int32 ns = data[p++]; // sectors
ss = data[p++]; // sector size
ss = (ss == 0xff) ? -1 : (128 << ss);
p += ns; // skip sector numbering map
if (cm) p += ns; // skip cylinder map
if (hm) p += ns; // skip head map
SS = new Int32[ns];
if (ss == -1) // sector size table
{
for (Int32 i = 0; i < ns; i++)
{
n = data[p++];
SS[i] = n + (data[p++] << 8);
}
}
// sector data
for (Int32 s = 0; s < ns; s++)
{
switch (data[p++]) // sector data type
{
case 1:
case 3:
case 5:
case 7:
p += (ss == -1) ? SS[s] : ss;
break;
case 2:
case 4:
case 6:
case 8:
p++;
break;
}
}
}
// read image (use last track's largest sector as default volume sector size)
if (ss == -1) for (Int32 i = 0; i < SS.Length; i++) if (SS[i] > ss) ss = SS[i];
for (p = 0; data[p] != 0x1a; p++) ; // count size of ASCII header
StringBuilder buf = new StringBuilder(p);
for (p = 0; data[p] != 0x1a; p++) buf.Append((Char)data[p]);
p++; // skip 0x1a header terminator
CHSVolume image = new CHSVolume(source, buf.ToString(), ss, ++nc, ++nh);
while (p < data.Length)
{
// track header
p++; // skip mode
Int32 c = data[p++]; // cylinder num
Int32 h = data[p++]; // head num
Boolean cm = ((h & 0x80) != 0);
Boolean hm = ((h & 0x40) != 0);
h &= 0x3f;
Int32 ns = data[p++]; // sectors
Track t = new Track(ns);
ss = data[p++]; // sector size
ss = (ss == 0xff) ? -1 : (128 << ss);
Int32[] SM = new Int32[ns];
for (Int32 i = 0; i < ns; i++) SM[i] = data[p++]; // sector numbering map
// TODO: don't skip these
if (cm) p += ns; // skip cylinder map
if (hm) p += ns; // skip head map
SS = new Int32[ns];
if (ss == -1) // sector size table
{
for (Int32 i = 0; i < ns; i++)
{
n = data[p++];
SS[i] = n + (data[p++] << 8);
}
}
// sector data
for (Int32 s = 0; s < ns; s++)
{
switch (data[p++]) // sector data type
{
case 1:
case 3:
case 5:
case 7:
n = (ss == -1) ? SS[s] : ss;
t[s] = new Sector(SM[s], n, data, p);
p += n;
break;
case 2:
case 4:
case 6:
case 8:
n = (ss == -1) ? SS[s] : ss;
t[s] = new Sector(SM[s], n, data[p++]);
break;
}
}
image[c, h] = t;
}
return image;
}
}
}