This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AosvsDumpFile.kt
389 lines (351 loc) · 15 KB
/
AosvsDumpFile.kt
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
384
385
386
387
388
389
@file:UseExperimental(ExperimentalUnsignedTypes::class)
/*
MIT License
Copyright (c) 2019 Stephen Merrony
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.
*/
import java.io.BufferedInputStream
import java.io.File
import java.io.FileOutputStream
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess
typealias dgWord = UShort
typealias dgDword = UInt
// const val DISK_BLOCK_BYTES = 512
/**
* A representation of the AOS/VS (and other) DUMP_II and DUMP_III format.
*/
class AosvsDumpFile(dumpFileStream: BufferedInputStream) {
private val dumpStream = dumpFileStream
private var loadIt = false
private var inFile = false
private var baseDir = ""
private var workingDir = ""
private val separator: String = File.separator
private lateinit var writeFile: FileOutputStream
private var totalFileSize = 0
private val knownEntryTypes = knownFstatEntryTypes()
private enum class RecordType(val id: Int) {
START(0),
FSB(1),
NB(2),
UDA(3),
ACL(4),
LINK(5),
START_BLOCK(6),
DATA_BLOCK(7),
END_BLOCK(8),
END(9),
Unknown(99); // This dummy value is returned when an unknown type is encountered.
companion object {
private val map = values().associateBy(RecordType::id)
fun fromInt(rt: Int) = map[rt] ?: Unknown
}
}
private data class RecordHeader(
val recordType: RecordType,
val recordLength: Int
)
private fun readHeader(): RecordHeader {
val twoBytes: ByteArray = readBlob(2, "header")
val rt = twoBytes[0].toInt().shr(2) and 0x00FF
val rl = (twoBytes[0].toInt() and 0x03).shl(8) + twoBytes[1].toInt()
return RecordHeader(RecordType.fromInt(rt), rl)
}
private data class SOD(
val header: RecordHeader,
val dumpFormatRevision: dgWord,
val dumpTimeSecs: dgWord,
val dumpTimeMins: dgWord,
val dumpTimeHours: dgWord,
val dumpTimeDay: dgWord,
val dumpTimeMonth: dgWord,
val dumpTimeYear: dgWord
)
private fun readSOD(): SOD {
val hdr = readHeader()
if (hdr.recordType != RecordType.START) {
println("ERROR: This does not appear to be an AOS/VS DUMP_II or DUMP_III file (No SOD record found).")
exitProcess(1)
}
val rev = readWord()
val secs = readWord()
val mins = readWord()
val hrs = readWord()
val day = readWord()
val mnth = readWord()
val year = readWord()
return SOD(hdr, rev, secs, mins, hrs, day, mnth, year)
}
private data class FstatEntryType(
val dgMnemonic: String,
val desc: String,
val isDir: Boolean,
val hasPayload: Boolean
)
private fun knownFstatEntryTypes() = mapOf(
0 to FstatEntryType(dgMnemonic = "FLNK", desc = "=>Link=>", isDir = false, hasPayload = false),
1 to FstatEntryType(dgMnemonic = "FDSF", desc = "System Data File", isDir = false, hasPayload = true),
2 to FstatEntryType(dgMnemonic = "FMTF", desc = "Mag Tape File", isDir = false, hasPayload = true),
3 to FstatEntryType(dgMnemonic = "FGFN", desc = "Generic File", isDir = false, hasPayload = true),
10 to FstatEntryType(dgMnemonic = "FDIR", desc = "<Directory>", isDir = true, hasPayload = false),
11 to FstatEntryType(dgMnemonic = "FLDU", desc = "<LDU Directory>", isDir = true, hasPayload = false),
12 to FstatEntryType(dgMnemonic = "FCPD", desc = "<Control Point Dir>", isDir = true, hasPayload = false),
64 to FstatEntryType(dgMnemonic = "FUDF", desc = "User Data File", isDir = false, hasPayload = true),
66 to FstatEntryType(dgMnemonic = "FUPD", desc = "User Profile", isDir = false, hasPayload = true),
67 to FstatEntryType(dgMnemonic = "FSTF", desc = "Symbol Table", isDir = false, hasPayload = true),
68 to FstatEntryType(dgMnemonic = "FTXT", desc = "Text File", isDir = false, hasPayload = true),
69 to FstatEntryType(dgMnemonic = "FLOG", desc = "System Log File", isDir = false, hasPayload = true),
74 to FstatEntryType(dgMnemonic = "FPRV", desc = "Program File", isDir = false, hasPayload = true),
87 to FstatEntryType(dgMnemonic = "FPRG", desc = "Program File", isDir = false, hasPayload = true)
)
private fun processNameBlock(
recHeader: RecordHeader,
fsbBlob: ByteArray,
summary: Boolean,
verbose: Boolean,
extract: Boolean,
ignoreErrors: Boolean
): String {
val fileType: String
val nameBytes: ByteArray = readBlob(recHeader.recordLength, "file name")
val fileName = nameBytes.toString(Charsets.US_ASCII).trimEnd('\u0000')
if (summary and verbose) println()
val thisEntry: FstatEntryType? = knownEntryTypes[fsbBlob[1].toInt()]
if (thisEntry == null) {
fileType = "Unknown File"
loadIt = true
} else {
fileType = thisEntry.desc
loadIt = thisEntry.hasPayload
if (thisEntry.isDir) {
workingDir += separator + fileName
if (extract) {
if (!File(workingDir).mkdirs()) {
println("ERROR: Could not create directory <$workingDir>")
if (!ignoreErrors) {
println("Giving up.")
exitProcess(1)
}
}
}
}
}
if (summary) {
val displayPath = if (workingDir.isEmpty()) fileName else File(workingDir).resolve(fileName).toString()
print("%-20s: ".format(fileType) + "%-48s".format(displayPath))
if (verbose || (thisEntry != null && thisEntry.isDir)) println()
else print("\t")
}
if (extract && loadIt) {
val writePath = if (workingDir.isEmpty()) fileName else workingDir + separator + fileName
if (verbose) println(" Creating file: $writePath")
try {
writeFile = FileOutputStream(writePath)
} catch (e: java.lang.Exception) {
println("ERROR: Could not create file <$writePath> due to ${e.message}")
if (!ignoreErrors) {
println("Giving up.")
exitProcess(1)
}
}
}
return fileName
}
private data class DataHeader(
val header: RecordHeader,
val byteAddress: dgDword,
val byteLength: dgDword,
val alignmentCount: dgWord
)
private fun processDataBlock(recHeader: RecordHeader, verbose: Boolean, extract: Boolean) {
val baBytes = readBlob(4, "byte address")
val ba: dgDword = baBytes[0].toUInt().shl(24) or
(baBytes[1].toUInt().shl(16) and 0x00ff0000U) or
(baBytes[2].toUInt().shl(8) and 0x0000ff00U) or
(baBytes[3].toUInt() and 0x000000ffU)
val blBytes = readBlob(4, "byte length")
val bl: dgDword = blBytes[0].toUInt().shl(24) or
(blBytes[1].toUInt().shl(16) and 0x00ff0000U) or
(blBytes[2].toUInt().shl(8) and 0x0000ff00U) or
(blBytes[3].toUInt() and 0x000000ffU)
val alnBytes = readBlob(2, "alignment count")
val ac: dgWord = (alnBytes[0].toUInt().shl(8) or alnBytes[1].toUInt()).toUShort()
val dhb = DataHeader(recHeader, ba, bl, ac)
if (verbose) println(" Data Block: ${dhb.byteLength} (bytes)")
// skip any alignment bytes - usually zero or one
if (ac > 0u) {
if (verbose) println(" Skipping ${dhb.alignmentCount} alignment byte(s)")
readBlob(ac.toInt(), "alignment byte(s)")
}
// large areas of NULLs may be skipped over by DUMP_II/III
// this is achieved by simply advancing the byte address so
// we must pad out if byte address is beyond end of last block
try {
if (dhb.byteAddress.toInt() > totalFileSize + 1) {
val paddingSize = dhb.byteAddress.toInt() - totalFileSize
// println("File Size: $totalFileSize, BA: ${dhb.byteAddress.toInt()}, Padding Size: $paddingSize")
val paddingBlock = ByteArray(paddingSize)
if (extract) {
if (verbose) println(" Padding with NULLs")
writeFile.write(paddingBlock)
}
totalFileSize += paddingSize
}
val blob = readBlob(dhb.byteLength.toInt(), "data blob")
if (extract) writeFile.write(blob)
} catch (e: Exception) {
println("ERROR: Count not write data to file due to ${e.message}")
exitProcess(1)
}
totalFileSize += dhb.byteLength.toInt()
inFile = true
}
private fun processEndBlock(verbose: Boolean, extract: Boolean, summary: Boolean) {
if (inFile) {
if (extract && loadIt) writeFile.close()
if (summary) println(" %12d bytes".format(totalFileSize))
totalFileSize = 0
inFile = false
} else {
// don't move above start dir for safety...
if (workingDir != baseDir) workingDir = Paths.get(workingDir).parent.toString()
if (verbose) println(" Popped dir - new dir is: $workingDir")
}
if (verbose) println("End Block Processed")
}
private fun processLink(
recHeader: RecordHeader,
linkName: String,
verbose: Boolean,
extract: Boolean,
summary: Boolean,
ignoreErrors: Boolean
) {
val linkTargetBA =
readBlob(recHeader.recordLength, "Link Target").dropLastWhile { it == 0.toByte() }.toByteArray()
var link = linkName
var linkTarget = linkTargetBA.toString(Charsets.US_ASCII)
// convert AOS/VS : directory separators to platform-specific ones and ensure upper case
linkTarget = linkTarget.replace(":", separator).toUpperCase()
if (summary || verbose) println(" -> Link Target: $linkTarget")
if (extract) {
val targetName: String
if (workingDir.isEmpty()) {
targetName = linkTarget
} else {
targetName = workingDir + separator + linkTarget
link = workingDir + separator + linkName
}
val linkPath = Paths.get(link)
val targetPath = Paths.get(targetName)
try {
Files.createSymbolicLink(linkPath, targetPath)
} catch (e: java.lang.Exception) {
println("ERROR: Could not create symbolic link")
if (!ignoreErrors) {
println("Giving up.")
exitProcess(1)
}
}
}
}
fun parse(
extract: Boolean,
ignoreErrors: Boolean,
list: Boolean,
summary: Boolean,
verbose: Boolean,
baseDir: String
) {
this.baseDir = baseDir
workingDir = baseDir
var fsbBlob = ByteArray(0)
var fileName = ""
// there should always be a SOD record...
val sod = readSOD()
if (summary or verbose) {
println("AOS/VS DUMP version : ${sod.dumpFormatRevision}")
println("DUMP date (y-m-d) : ${sod.dumpTimeYear}-${sod.dumpTimeMonth}-${sod.dumpTimeDay}")
println("DUMP time (hh:mm:ss) : ${sod.dumpTimeHours}:${sod.dumpTimeMins}:${sod.dumpTimeSecs}")
}
// now work through the dump examining each block type and handling accordingly...
var done = false
while (!done) {
val recHdr = readHeader()
if (verbose) println("Found block of type: ${recHdr.recordType.name} length: ${recHdr.recordLength}")
when (recHdr.recordType) {
RecordType.START -> {
println("ERROR: Another START record found in DUMP - this should not happen.")
exitProcess(1)
}
RecordType.FSB -> {
fsbBlob = readBlob(recHdr.recordLength, "FSB")
loadIt = false
}
RecordType.NB -> {
fileName = processNameBlock(recHdr, fsbBlob, summary, verbose, extract, ignoreErrors)
}
RecordType.UDA -> {
// throw away for now
readBlob(recHdr.recordLength, "UDA")
}
RecordType.ACL -> {
val aclBlob = readBlob(recHdr.recordLength, "ACL")
if (verbose) println(" ACL: " + aclBlob.toString(Charsets.US_ASCII).trimEnd('\u0000'))
}
RecordType.LINK -> {
processLink(recHdr, fileName, verbose, extract, summary, ignoreErrors)
}
RecordType.START_BLOCK -> {
// nothing to do - it's just a recHdr
}
RecordType.DATA_BLOCK -> {
processDataBlock(recHdr, verbose, extract)
}
RecordType.END_BLOCK -> {
processEndBlock(verbose, extract, summary)
}
RecordType.END -> {
println("=== End of Dump ===")
done = true
}
RecordType.Unknown -> {
println("ERROR: Unknown block type in DUMP file. Giving up.")
exitProcess(1)
}
}
}
}
// helper functions...
private fun readBlob(len: Int, desc: String): ByteArray {
val blob = ByteArray(len)
try {
val n = dumpStream.read(blob)
check(n == len) { "Did not get expected number of bytes" }
} catch (e: Exception) {
println("ERROR: Could not read $desc record - ${e.message}")
exitProcess(1)
}
return blob
}
private fun readWord(): dgWord {
val twoBytes: ByteArray = readBlob(2, "DG Word")
return (twoBytes[0].toUInt().shl(8)).toUShort().or(twoBytes[1].toUShort())
}
}