-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhfsDrive.cc
173 lines (142 loc) · 5.04 KB
/
hfsDrive.cc
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
// hfsDrive.cc
#include <iostream>
#include <iomanip>
#include <time.h>
#include "hfsDrive.h"
#include "hfsUtils.h"
using namespace std;
HfsDrive::HfsDrive()
: _backingStore(NULL), _driveInfo(NULL), _primaryHeader(NULL), _secondaryHeader(NULL),
_catalogFile(NULL)
{
}
void HfsDrive::mount(const char* filename) {
if (this->_backingStore != NULL) {
cout << "HfsDrive already mounted. Use umount before calling this method again." << endl;
return;
}
// use ios::ate to position file pointer at the end of the file
this->_backingStore = new ifstream(filename, ios::in|ios::binary|ios::ate);
this->_storeSize = this->_backingStore->tellg();
cout << "Total file size: " << this->_storeSize << endl;
this->readPrimaryVolumeHeader();
this->readSecondaryVolumeHeader();
this->_driveInfo = new HfsDriveInfo;
this->_driveInfo->startOffset = HfsDrive::startOffset;
this->_driveInfo->blockSize = this->_primaryHeader->blockSize;
this->_catalogFile = new HfsCatalogFile(this->_driveInfo, this->_backingStore, &(this->_primaryHeader->catalogFile));
//cout << "Catalog file header: " << endl;
//this->_catalogFile->dumpHeaderRec();
}
void HfsDrive::unmount() {
if (this->_backingStore == NULL) {
cout << "HfsDrive not mounted. Use mount before calling this method again." << endl;
return;
}
this->_backingStore->close();
delete this->_backingStore;
this->_backingStore = NULL;
if (this->_primaryHeader != NULL) {
delete this->_primaryHeader;
this->_primaryHeader = NULL;
}
if (this->_secondaryHeader != NULL) {
delete this->_secondaryHeader;
this->_secondaryHeader = NULL;
}
if (this->_catalogFile != NULL) {
delete this->_catalogFile;
this->_catalogFile = NULL;
}
}
void HfsDrive::dumpPrimaryHeader() {
this->dumpVolumeHeader("primary", this->_primaryHeader);
}
void HfsDrive::dumpSecondaryHeader() {
this->dumpVolumeHeader("secondary", this->_secondaryHeader);
}
void HfsDrive::dumpVolumeHeader(const char* name, HFSPlusVolumeHeader* header) {
char signatureBuffer[] = " ";
char lmvBuffer[] = " ";
time_t aTime;
cout << setfill('0');
cout << "Dumping volume header: " << name << endl
<< hex
<< " signature: " << setw(4) << header->signature
<< " (" << this->toASCII(header->signature, signatureBuffer) << ")" << endl
<< " version: " << setw(4) << header-> version << endl
<< " attributes: " << setw(8) << header->attributes << endl
<< " last mounted version: " << header->lastMountedVersion
<< " (" << this->toASCII(header->lastMountedVersion, lmvBuffer) << ")" << endl
<< dec
<< " journal info block: " << header->journalInfoBlock << endl
<< endl;
aTime = to_bsd_time(header->createDate);
cout << " create date: " << ctime(&aTime);
aTime = to_bsd_time(header->modifyDate);
cout << " modify date: " << ctime(&aTime);
aTime = to_bsd_time(header->backupDate);
cout << " backup date: " << ctime(&aTime);
aTime = to_bsd_time(header->checkedDate);
cout << " checked date: " << ctime(&aTime)
<< endl;
cout << " file count: " << header->fileCount << endl
<< " folder count: " << header->folderCount << endl
<< endl
<< " block size: " << header->blockSize << endl
<< " total blocks: " << header->totalBlocks << endl
<< " free blocks: " << header->freeBlocks << endl
<< endl
<< " next allocation: " << header->nextAllocation << endl
<< " resource clump size: " << header->rsrcClumpSize << endl
<< " data clump size: " << header->dataClumpSize << endl
<< " next catalog id: " << header->nextCatalogID << endl
<< endl
<< " write count: " << header->writeCount << endl
<< hex
<< " encodings bitmap: " << setw(8) << header->encodingsBitmap << endl
<< endl;
}
char* HfsDrive::toASCII(u_int16_t value, char* buffer) {
buffer[1] = (char)(value & 0x00FF);
buffer[0] = (char)(value >> 8);
return buffer;
}
char* HfsDrive::toASCII(u_int32_t value, char* buffer) {
this->toASCII((u_int16_t)(value & 0x0000FFFF), buffer + (2 * sizeof(char)));
this->toASCII((u_int16_t)(value >> 16), buffer);
return buffer;
}
streamsize HfsDrive::readPrimaryVolumeHeader() {
if (this->_primaryHeader != NULL) {
delete this->_primaryHeader;
}
this->_primaryHeader = new HFSPlusVolumeHeader();
this->_backingStore->seekg(HfsDrive::startOffset + HfsDrive::sectorSize * 2, ios::beg);
return this->readVolumeHeader(this->_primaryHeader);
}
streamsize HfsDrive::readSecondaryVolumeHeader() {
if (this->_secondaryHeader != NULL) {
delete this->_secondaryHeader;
}
this->_secondaryHeader = new HFSPlusVolumeHeader();
this->_backingStore->seekg(-HfsDrive::sectorSize * 2, ios::end);
return this->readVolumeHeader(this->_secondaryHeader);
}
streamsize HfsDrive::readVolumeHeader(HFSPlusVolumeHeader* header) {
this->_backingStore->read((char*)header, sizeof(HFSPlusVolumeHeader));
byteSwapVolumeHeader(header);
return this->_backingStore->gcount();
}
u_int32_t HfsDrive::getBlockSize() {
if (this->_primaryHeader == NULL) {
if (this->_secondaryHeader == NULL) {
return 0;
}
else {
return this->_secondaryHeader->blockSize;
}
} else {
return this->_primaryHeader->blockSize;
}
}