summaryrefslogtreecommitdiffstats
path: root/libpit/Source/libpit.cpp
blob: 030a80bf0b507bde54341cde80d129b828e927f7 (plain) (blame)
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
/* Copyright (c) 2010 Benjamin Dobell, Glass Echidna
 
 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.*/

// libpit
#include "libpit.h"

using namespace libpit;

PitEntry::PitEntry()
{
	unused = false;
	partitionType = 0;
	partitionIdentifier = 0;
	partitionFlags = 0;
	unknown1 = 0;
	partitionBlockSize = 0;
	partitionBlockCount = 0;
	unknown2 = 0;
	unknown3 = 0;

	memset(partitionName, 0, 32);
	memset(filename, 0, 64);
}

PitEntry::~PitEntry()
{
}



PitData::PitData()
{
	entryCount = 0;

	unknown1 = 0;
	unknown2 = 0;

	unknown3 = 0;
	unknown4 = 0;

	unknown5 = 0;
	unknown6 = 0;

	unknown7 = 0;
	unknown8 = 0;
}

PitData::~PitData()
{
	for (unsigned int i = 0; i < entries.size(); i++)
		delete entries[i];
}

bool PitData::Unpack(const unsigned char *data)
{
	if (PitData::UnpackInteger(data, 0) != PitData::kFileIdentifier)
		return (false);

	// Remove existing entries
	for (unsigned int i = 0; i < entries.size(); i++)
		delete entries[i];

	entryCount = PitData::UnpackInteger(data, 4);

	entries.resize(entryCount);

	unknown1 = PitData::UnpackInteger(data, 8);
	unknown2 = PitData::UnpackInteger(data, 12);

	unknown3 = PitData::UnpackShort(data, 16);
	unknown4 = PitData::UnpackShort(data, 18);

	unknown5 = PitData::UnpackShort(data, 20);
	unknown6 = PitData::UnpackShort(data, 22);

	unknown7 = PitData::UnpackShort(data, 24);
	unknown8 = PitData::UnpackShort(data, 26);

	unsigned int integerValue;
	unsigned int entryOffset;

	for (unsigned int i = 0; i < entryCount; i++)
	{
		entryOffset = PitData::kHeaderDataSize + i * PitEntry::kDataSize;

		entries[i] = new PitEntry();

		integerValue = PitData::UnpackInteger(data, entryOffset);
		entries[i]->SetUnused((integerValue != 0) ? true : false);

		integerValue = PitData::UnpackInteger(data, entryOffset + 4);
		entries[i]->SetPartitionType(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 8);
		entries[i]->SetPartitionIdentifier(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 12);
		entries[i]->SetPartitionFlags(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 16);
		entries[i]->SetUnknown1(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 20);
		entries[i]->SetPartitionBlockSize(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 24);
		entries[i]->SetPartitionBlockCount(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 28);
		entries[i]->SetUnknown2(integerValue);

		integerValue = PitData::UnpackInteger(data, entryOffset + 32);
		entries[i]->SetUnknown3(integerValue);

		entries[i]->SetPartitionName((const char *)data + entryOffset + 36);
		entries[i]->SetFilename((const char *)data + entryOffset + 36 + PitEntry::kPartitionNameMaxLength);
	}

	return (true);
}

void PitData::Pack(unsigned char *data) const
{
	PitData::PackInteger(data, 0, PitData::kFileIdentifier);

	PitData::PackInteger(data, 4, entryCount);

	PitData::PackInteger(data, 8, unknown1);
	PitData::PackInteger(data, 12, unknown2);

	PitData::PackShort(data, 16, unknown3);
	PitData::PackShort(data, 18, unknown4);

	PitData::PackShort(data, 20, unknown5);
	PitData::PackShort(data, 22, unknown6);

	PitData::PackShort(data, 24, unknown7);
	PitData::PackShort(data, 26, unknown8);

	int entryOffset;

	for (unsigned int i = 0; i < entryCount; i++)
	{
		entryOffset = PitData::kHeaderDataSize + i * PitEntry::kDataSize;

		PitData::PackInteger(data, entryOffset, (entries[i]->GetUnused()) ? 1 : 0);

		PitData::PackInteger(data, entryOffset + 4, entries[i]->GetPartitionType());
		PitData::PackInteger(data, entryOffset + 8, entries[i]->GetPartitionIdentifier());
		PitData::PackInteger(data, entryOffset + 12, entries[i]->GetPartitionFlags());

		PitData::PackInteger(data, entryOffset + 16, entries[i]->GetUnknown1());

		PitData::PackInteger(data, entryOffset + 20, entries[i]->GetPartitionBlockSize());
		PitData::PackInteger(data, entryOffset + 24, entries[i]->GetPartitionBlockCount());

		PitData::PackInteger(data, entryOffset + 28, entries[i]->GetUnknown2());
		PitData::PackInteger(data, entryOffset + 32, entries[i]->GetUnknown3());

		memcpy(data + entryOffset + 36, entries[i]->GetPartitionName(), PitEntry::kPartitionNameMaxLength);
		memcpy(data + entryOffset + 36 + PitEntry::kPartitionNameMaxLength, entries[i]->GetPartitionName(), PitEntry::kFilenameMaxLength);
	}
}

void PitData::Clear(void)
{
	entryCount = 0;

	unknown1 = 0;
	unknown2 = 0;

	unknown3 = 0;
	unknown4 = 0;

	unknown5 = 0;
	unknown6 = 0;

	unknown7 = 0;
	unknown8 = 0;

	for (unsigned int i = 0; i < entries.size(); i++)
		delete entries[i];

	entries.clear();
}

PitEntry *PitData::GetEntry(unsigned int index)
{
	return (entries[index]);
}

const PitEntry *PitData::GetEntry(unsigned int index) const
{
	return (entries[index]);
}

PitEntry *PitData::FindEntry(const char *partitionName)
{
	for (unsigned int i = 0; i < entries.size(); i++)
	{
		if (!entries[i]->GetUnused() && strcmp(entries[i]->GetPartitionName(), partitionName) == 0)
			return (entries[i]);
	}

	return (nullptr);
}

const PitEntry *PitData::FindEntry(const char *partitionName) const
{
	for (unsigned int i = 0; i < entries.size(); i++)
	{
		if (!entries[i]->GetUnused() && strcmp(entries[i]->GetPartitionName(), partitionName) == 0)
			return (entries[i]);
	}

	return (nullptr);
}

PitEntry *PitData::FindEntry(unsigned int partitionIdentifier)
{
	for (unsigned int i = 0; i < entries.size(); i++)
	{
		if (!entries[i]->GetUnused() && entries[i]->GetPartitionIdentifier() == partitionIdentifier)
			return (entries[i]);
	}

	return (nullptr);
}

const PitEntry *PitData::FindEntry(unsigned int partitionIdentifier) const
{
	for (unsigned int i = 0; i < entries.size(); i++)
	{
		if (!entries[i]->GetUnused() && entries[i]->GetPartitionIdentifier() == partitionIdentifier)
			return (entries[i]);
	}

	return (nullptr);
}