-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxmachofat.cpp
238 lines (176 loc) · 6.71 KB
/
xmachofat.cpp
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
/* Copyright (c) 2017-2024 hors<[email protected]>
*
* 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.
*/
#include "xmachofat.h"
XMACHOFat::XMACHOFat(QIODevice *pDevice) : XArchive(pDevice)
{
}
bool XMACHOFat::isValid(PDSTRUCT *pPdStruct)
{
Q_UNUSED(pPdStruct)
bool bResult = false;
quint32 nMagic = read_uint32(0);
if ((nMagic == XMACH_DEF::S_FAT_MAGIC) || (nMagic == XMACH_DEF::S_FAT_CIGAM)) {
bResult = (getNumberOfRecords(pPdStruct) < 10); // TODO Check !!!
}
return bResult;
}
bool XMACHOFat::isValid(QIODevice *pDevice)
{
XMACHOFat xmachofat(pDevice);
return xmachofat.isValid();
}
XBinary::ENDIAN XMACHOFat::getEndian()
{
ENDIAN result = ENDIAN_UNKNOWN;
quint32 nData = read_uint32(0);
if (nData == XMACH_DEF::S_FAT_MAGIC) {
result = ENDIAN_LITTLE;
} else if (nData == XMACH_DEF::S_FAT_CIGAM) {
result = ENDIAN_BIG;
}
return result;
}
quint64 XMACHOFat::getNumberOfRecords(PDSTRUCT *pPdStruct)
{
Q_UNUSED(pPdStruct)
return read_uint32(offsetof(XMACH_DEF::fat_header, nfat_arch), isBigEndian());
}
QList<XArchive::RECORD> XMACHOFat::getRecords(qint32 nLimit, PDSTRUCT *pPdStruct)
{
XBinary::PDSTRUCT pdStructEmpty = {};
if (!pPdStruct) {
pdStructEmpty = XBinary::createPdStruct();
pPdStruct = &pdStructEmpty;
}
QList<RECORD> listResult;
qint32 nNumberOfRecords = (qint32)getNumberOfRecords(pPdStruct);
if (nLimit != -1) {
nNumberOfRecords = qMin(nNumberOfRecords, nLimit);
}
bool bIsBigEndian = isBigEndian();
QMap<quint64, QString> mapCpuTypes = XMACH::getHeaderCpuTypesS();
for (qint32 i = 0; (i < nNumberOfRecords) && (!(pPdStruct->bIsStop)); i++) {
qint64 nOffset = sizeof(XMACH_DEF::fat_header) + i * sizeof(XMACH_DEF::fat_arch);
quint32 _cputype = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, cputype), bIsBigEndian);
quint32 _cpusubtype = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, cpusubtype), bIsBigEndian);
quint32 _offset = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, offset), bIsBigEndian);
quint32 _size = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, size), bIsBigEndian);
// quint32 _align=read_uint32(nOffset+offsetof(XMACH_DEF::fat_arch,align),bIsBigEndian);
RECORD record = {};
record.sFileName = XMACH::_getArch(_cputype, _cpusubtype);
record.nHeaderOffset = nOffset;
record.nHeaderSize = sizeof(XMACH_DEF::fat_arch);
record.nDataOffset = _offset;
record.nCompressedSize = _size;
record.nUncompressedSize = _size;
record.compressMethod = COMPRESS_METHOD_STORE;
listResult.append(record);
}
return listResult;
}
XBinary::OSINFO XMACHOFat::getOsInfo()
{
XBinary::OSINFO result = {};
result.osName = OSNAME_MACOS;
result.sArch = getArch();
result.mode = getMode();
result.sType = typeIdToString(getType());
result.endian = getEndian();
return result;
}
QString XMACHOFat::getFileFormatExt()
{
return "";
}
qint64 XMACHOFat::getFileFormatSize(PDSTRUCT *pPdStruct)
{
return _calculateRawSize(pPdStruct);
}
QString XMACHOFat::getFileFormatString()
{
QString sResult;
sResult = QString("MACHOFAT");
// TODO more info, number of records
return sResult;
}
QList<XBinary::MAPMODE> XMACHOFat::getMapModesList()
{
QList<MAPMODE> listResult;
listResult.append(MAPMODE_REGIONS);
return listResult;
}
XBinary::_MEMORY_MAP XMACHOFat::getMemoryMap(MAPMODE mapMode, PDSTRUCT *pPdStruct)
{
Q_UNUSED(mapMode)
XBinary::PDSTRUCT pdStructEmpty = {};
if (!pPdStruct) {
pdStructEmpty = XBinary::createPdStruct();
pPdStruct = &pdStructEmpty;
}
Q_UNUSED(pPdStruct)
XBinary::_MEMORY_MAP result = {};
result.endian = getEndian();
result.nBinarySize = getSize();
bool bIsBigEndian = (result.endian == ENDIAN_BIG);
qint32 nIndex = 0;
{
_MEMORY_RECORD record = {};
record.nIndex = nIndex++;
record.type = MMT_HEADER;
record.nOffset = 0;
record.nSize = sizeof(XMACH_DEF::fat_header);
record.nAddress = -1;
record.sName = tr("Header");
result.listRecords.append(record);
}
quint32 nNumberOfRecords = read_uint32(offsetof(XMACH_DEF::fat_header, nfat_arch), bIsBigEndian);
for (qint32 i = 0; i < (qint32)nNumberOfRecords; i++) {
_MEMORY_RECORD record = {};
qint64 nOffset = sizeof(XMACH_DEF::fat_header) + i * sizeof(XMACH_DEF::fat_arch);
quint32 _cputype = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, cputype), bIsBigEndian);
quint32 _cpusubtype = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, cpusubtype), bIsBigEndian);
quint32 _offset = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, offset), bIsBigEndian);
quint32 _size = read_uint32(nOffset + offsetof(XMACH_DEF::fat_arch, size), bIsBigEndian);
record.sName = XMACH::_getArch(_cputype, _cpusubtype);
record.nOffset = _offset;
record.nSize = _size;
record.nAddress = -1;
record.type = MMT_LOADSEGMENT;
result.listRecords.append(record);
}
return result;
}
QString XMACHOFat::getArch()
{
return tr("Universal");
}
qint32 XMACHOFat::getType()
{
return TYPE_BUNDLE;
}
QString XMACHOFat::typeIdToString(qint32 nType)
{
QString sResult = tr("Unknown");
switch (nType) {
case TYPE_BUNDLE: sResult = tr("Bundle");
}
return sResult;
}