MacDiskExtractor/disk.cpp

301 lines
9.6 KiB
C++
Raw Normal View History

2016-02-10 11:58:01 +00:00
#include <iostream>
#include <fstream>
#include <string>
2016-03-04 11:40:04 +00:00
#include <vector>
2016-02-10 11:58:01 +00:00
#include <iomanip>
#include <sstream>
2016-02-10 11:58:01 +00:00
2016-03-18 12:10:49 +00:00
#define LOGBLOCK_LENGTH 512
#define ALLOCBLOCK_LENGTH LOGBLOCK_LENGTH * 2
#define VINF_OFFSET LOGBLOCK_LENGTH * 2
#define ABMAP_OFFSET VINF_OFFSET + 64
#define FDIR_OFFSET LOGBLOCK_LENGTH * 4
2016-02-10 11:58:01 +00:00
using namespace std;
class Disk {
2016-02-19 10:02:09 +00:00
fstream disk;
2016-03-18 12:10:49 +00:00
2016-02-19 10:02:09 +00:00
Disk() {
disk.open("copy.dsk", ios_base::binary | ios_base::in);
2016-03-18 12:10:49 +00:00
disk.seekg(VINF_OFFSET + 16);
drBILen = readByte(2);
2018-04-25 13:08:37 +00:00
drNmAIBIks = readByte(2);
2016-03-18 12:10:49 +00:00
allocBlockMap.resize(drNmAIBIks);
disk.seekg(ABMAP_OFFSET);
int temp, val1, val2;
for (int i = 0; i < drNmAIBIks;) {
temp = readByte(3);
val1 = (temp & 0x00FFF000) >> 12;
val2 = (temp & 0x00000FFF);
allocBlockMap[i++] = val1;
allocBlockMap[i++] = val2;
}
2016-02-19 10:02:09 +00:00
}
2016-02-10 11:58:01 +00:00
2016-02-19 10:02:09 +00:00
~Disk() {
disk.close();
}
Disk(const Disk&) = delete;
Disk& operator=(const Disk&) = delete;
2016-02-10 11:58:01 +00:00
public:
2016-03-18 12:10:49 +00:00
int drBILen;
int drNmAIBIks;
vector<int> allocBlockMap;
int readByte(int size) {
2016-02-19 10:02:09 +00:00
int res = 0;
for (int i = 0; i < size; ++i) {
res = res << 8;
2016-03-18 12:10:49 +00:00
res |= disk.get();
2016-02-19 10:02:09 +00:00
}
return res;
}
2016-03-18 12:10:49 +00:00
string readString(int size) {
char *temp = new char[size + 1];
disk.get(temp, size + 1);
string stemp(temp);
delete temp;
return stemp;
}
static fstream& getDiskStream() {
return getDisk().disk;
}
static Disk& getDisk() {
2016-02-19 10:02:09 +00:00
static Disk _this;
2016-03-18 12:10:49 +00:00
return _this;
}
static int getFContOffset() {
return FDIR_OFFSET + LOGBLOCK_LENGTH * getDisk().drBILen;
2016-02-19 10:02:09 +00:00
}
2016-03-04 11:40:04 +00:00
static int findFile(int offset) {
2016-03-18 12:10:49 +00:00
Disk::getDiskStream().seekg(offset);
while (!(Disk::getDisk().readByte(1) && 0x80)) {
2016-03-04 11:40:04 +00:00
++offset;
}
return offset;
}
static void volumeInfo() {
2016-03-18 12:10:49 +00:00
int temp;
2018-04-25 13:08:37 +00:00
Disk::getDiskStream().seekg(512 * 2);
2016-03-04 11:40:04 +00:00
cout << hex;
cout << "--------------------" << endl;
2016-03-18 12:10:49 +00:00
cout << "Volume Information" << endl;
cout << "--------------------" << endl;
cout << "always $D2D7 " << Disk::getDisk().readByte(2) << endl;
cout << "date and time of initialization " << Disk::getDisk().readByte(4) << endl;
cout << "date and time of last backup " << Disk::getDisk().readByte(4) << endl;
cout << "volume attributes " << Disk::getDisk().readByte(2) << endl;
cout << "number of files in directory " << Disk::getDisk().readByte(2) << endl;
cout << "first block of directory " << Disk::getDisk().readByte(2) << endl;
cout << "length of directory in blocks " << Disk::getDisk().readByte(2) << endl;
cout << "number of allocation blocks on volume " << Disk::getDisk().readByte(2) << endl;
cout << "size of allocation blocks " << Disk::getDisk().readByte(4) << endl;
cout << "number of bytes to allocate " << Disk::getDisk().readByte(4) << endl;
cout << "first allocation block in block map " << Disk::getDisk().readByte(2) << endl;
cout << "next unused file number " << Disk::getDisk().readByte(4) << endl;
cout << "number of unused allocation blocks " << Disk::getDisk().readByte(2) << endl;
temp = Disk::getDisk().readByte(1);
cout << "length of volume name " << temp << endl;
cout << "characters of volume name " << Disk::getDisk().readString(temp) << endl;
cout << "--------------------" << endl;
cout << "Allocation Block Map" << endl;
2016-03-04 11:40:04 +00:00
cout << "--------------------" << endl;
2016-03-18 12:10:49 +00:00
int i = 0;
for (auto &it : getDisk().allocBlockMap) {
cout << 2 + i++ << "->" << it << " ";
if (it == 1) {
cout << endl;
}
}
cout << endl << "--------------------" << endl;
2016-03-04 11:40:04 +00:00
}
2016-02-19 10:02:09 +00:00
};
2016-03-18 12:10:49 +00:00
/* ------------------------------------------------------------------------- */
2016-02-19 10:02:09 +00:00
class File {
public:
2016-03-04 11:40:04 +00:00
2016-02-19 10:02:09 +00:00
int flFIags;
int flTyp;
int flUsrWds;
int flFINum;
int flStBlk;
int flLgLen;
int flPyLen;
int flRStBlk;
int flRLgLen;
int flRPyLen;
int flCrDat;
int flMdDat;
int flNam;
string flNamS;
int fBegin;
int fEnd;
File(int offset) {
2016-03-18 12:10:49 +00:00
Disk::getDiskStream().seekg(offset);
flFIags = Disk::getDisk().readByte(1);
2016-03-04 11:40:04 +00:00
if (flFIags & 0x80) {
2016-03-18 12:10:49 +00:00
flTyp = Disk::getDisk().readByte(1);
flUsrWds = Disk::getDisk().readByte(16);
flFINum = Disk::getDisk().readByte(4);
flStBlk = Disk::getDisk().readByte(2);
flLgLen = Disk::getDisk().readByte(4);
flPyLen = Disk::getDisk().readByte(4);
flRStBlk = Disk::getDisk().readByte(2);
flRLgLen = Disk::getDisk().readByte(4);
flRPyLen = Disk::getDisk().readByte(4);
flCrDat = Disk::getDisk().readByte(4);
flMdDat = Disk::getDisk().readByte(4);
flNam = Disk::getDisk().readByte(1);
flNamS = Disk::getDisk().readString(flNam);
2016-02-19 10:02:09 +00:00
fBegin = offset;
fEnd = fBegin + 51 + flNam;
if (fEnd % 2) {
++fEnd;
}
}
}
string printAll() {
stringstream temp;
temp << hex;
temp << "--------------------" << endl;
temp << "flags " << flFIags << endl;
temp << "version number " << flTyp << endl;
temp << "information used by the Finder " << flUsrWds << endl;
temp << "file number " << flFINum << endl;
temp << "first allocation block of data fork " << flStBlk << endl;
temp << "logical end-of-file of data fork " << flLgLen << endl;
temp << "physical end-of-file of data fork " << flPyLen << endl;
temp << "first allocation block of resource fork " << flRStBlk << endl;
temp << "logical end-of-file of resource fork " << flRLgLen << endl;
temp << "physical end-of-file of resource fork " << flRPyLen << endl;
temp << "date and time of creation " << flCrDat << endl;
temp << "date and time of last modification " << flMdDat << endl;
temp << "length of file name " << flNam << endl;
temp << "--------------------" << endl;
return temp.str();
2016-03-18 12:10:49 +00:00
}
string printFileName() {
return string("File name: ") + flNamS;
2016-03-18 12:10:49 +00:00
}
string getValidFileName() {
string temp = flNamS;
auto it = temp.find("/");
while (it < temp.size()) {
temp.replace(it, 1, "_");
it = temp.find("/");
}
return temp;
2016-02-19 10:02:09 +00:00
}
void saveData(fstream& out, int point) {
int firstOffset = Disk::getFContOffset();
char str[ALLOCBLOCK_LENGTH + 1];
while ((point != 1) && (point != 0)) {
2018-04-25 13:08:37 +00:00
int offset = firstOffset + (point - 2) * ALLOCBLOCK_LENGTH;
//cout << hex << "block " << point << " offset " << offset << "\n";
Disk::getDiskStream().seekg(offset);
Disk::getDiskStream().read(str, ALLOCBLOCK_LENGTH);
out.write(str, ALLOCBLOCK_LENGTH);
//cout.write(str, ALLOCBLOCK_LENGTH);
point = Disk::getDisk().allocBlockMap[point - 2];
}
}
2016-03-18 12:10:49 +00:00
void saveFile(string path) {
2018-04-25 13:08:37 +00:00
string fname;
if (flRStBlk != 0) {
2018-04-25 13:08:37 +00:00
fname = path + getValidFileName() + ".resourсe";
//cout << fname << "\n";
fstream file(fname, ios_base::binary | ios_base::out);
if (!file.is_open()) {
cout << "error: file not created: " << flNamS << endl;
}
saveData(file, flRStBlk);
file.close();
}
if (flStBlk != 0) {
2018-04-25 13:08:37 +00:00
fname = path + getValidFileName() + ".data";
//cout << fname << "\n";
fstream file(fname, ios_base::binary | ios_base::out);
if (!file.is_open()) {
cout << "error: file not created: " << flNamS << endl;
}
saveData(file, flStBlk);
file.close();
2016-03-18 12:10:49 +00:00
}
2016-02-19 10:02:09 +00:00
}
2016-02-10 11:58:01 +00:00
};
void generInfoFile(string path, vector< File > files) {
fstream file(path + "Files.info", ios_base::binary | ios_base::out);
if (!file.is_open()) {
cout << "error: info-file not created: " << endl;
}
file << hex;
for (int i = 0; i < files.size(); ++i) {
file << files[i].printFileName() << endl;
file << files[i].printAll() << endl;
file << "firstOffset resource fork: " << (!files[i].flRStBlk ? 0 : Disk::getFContOffset() + (files[i].flRStBlk - 2) * ALLOCBLOCK_LENGTH) << endl;
file << "firstOffset data fork: " << (!files[i].flStBlk ? 0 : Disk::getFContOffset() + (files[i].flStBlk - 2) * ALLOCBLOCK_LENGTH) << endl;
file << endl << endl;
}
file.close();
}
2016-03-04 11:40:04 +00:00
int main() {
2016-03-18 12:10:49 +00:00
Disk::getDiskStream().seekg(VINF_OFFSET + 12);
int fileCount = Disk::getDisk().readByte(2);
2016-03-04 11:40:04 +00:00
vector< File > files;
2016-02-19 10:02:09 +00:00
2016-03-04 11:40:04 +00:00
int fBegin = FDIR_OFFSET;
for(int i = 0; i < fileCount; ++i) {
fBegin = Disk::findFile(fBegin);
files.emplace_back(fBegin);
fBegin = files.back().fEnd;
2016-02-19 10:02:09 +00:00
}
2016-03-04 11:40:04 +00:00
Disk::volumeInfo();
2016-03-18 12:10:49 +00:00
system("rm -r Files");
system("mkdir Files");
for (int i = 0; i < fileCount; ++i) {
files[i].saveFile("Files/");
2016-03-18 12:10:49 +00:00
}
generInfoFile("Files/", files);
2016-03-18 12:10:49 +00:00
2016-02-19 10:02:09 +00:00
return 1;
2018-04-25 13:08:37 +00:00
}