-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxmsdos.cpp
643 lines (494 loc) · 14.7 KB
/
xmsdos.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// copyright (c) 2017-2020 hors<horsicq@gmail.com>
//
// 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 "xmsdos.h"
XMSDOS::XMSDOS(QIODevice *__pDevice, bool bIsImage, qint64 nImageBase): XBinary(__pDevice,bIsImage,nImageBase)
{
}
bool XMSDOS::isValid()
{
bool bResult=false;
quint16 magic=get_magic();
if( (magic==XMSDOS_DEF::S_IMAGE_DOS_SIGNATURE)||
(magic==XMSDOS_DEF::S_IMAGE_DOS_SIGNATURE_ZM))
{
bResult=true;
}
return bResult;
}
quint16 XMSDOS::get_magic()
{
return read_uint16((qint64)offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_magic));
}
qint32 XMSDOS::get_lfanew()
{
return read_int32(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_lfanew));
}
qint64 XMSDOS::getDosHeaderOffset()
{
return 0;
}
qint64 XMSDOS::getDosHeaderSize()
{
return sizeof(XMSDOS_DEF::IMAGE_DOS_HEADER);
}
qint64 XMSDOS::getDosHeaderExOffset()
{
return 0;
}
qint64 XMSDOS::getDosHeaderExSize()
{
return sizeof(XMSDOS_DEF::IMAGE_DOS_HEADEREX);
}
XMSDOS_DEF::IMAGE_DOS_HEADER XMSDOS::getDosHeader()
{
XMSDOS_DEF::IMAGE_DOS_HEADER result={};
read_array((qint64)offsetof(XMSDOS_DEF::IMAGE_DOS_HEADER,e_magic),(char *)&result,sizeof(XMSDOS_DEF::IMAGE_DOS_HEADER));
return result;
}
XMSDOS_DEF::IMAGE_DOS_HEADEREX XMSDOS::getDosHeaderEx()
{
// TODO
XMSDOS_DEF::IMAGE_DOS_HEADEREX result={};
read_array((qint64)offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_magic),(char *)&result,sizeof(XMSDOS_DEF::IMAGE_DOS_HEADEREX));
return result;
}
void XMSDOS::setDosHeader(XMSDOS_DEF::IMAGE_DOS_HEADER *pDosHeader)
{
// TODO
write_array((qint64)offsetof(XMSDOS_DEF::IMAGE_DOS_HEADER,e_magic),(char *)pDosHeader,sizeof(XMSDOS_DEF::IMAGE_DOS_HEADER));
}
void XMSDOS::setDosHeaderEx(XMSDOS_DEF::IMAGE_DOS_HEADEREX *pDosHeaderEx)
{
write_array((qint64)offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_magic),(char *)pDosHeaderEx,sizeof(XMSDOS_DEF::IMAGE_DOS_HEADEREX));
}
void XMSDOS::set_e_magic(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_magic),nValue);
}
void XMSDOS::set_e_cblp(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cblp),nValue);
}
void XMSDOS::set_e_cp(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cp),nValue);
}
void XMSDOS::set_e_crlc(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_crlc),nValue);
}
void XMSDOS::set_e_cparhdr(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cparhdr),nValue);
}
void XMSDOS::set_e_minalloc(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_minalloc),nValue);
}
void XMSDOS::set_e_maxalloc(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_maxalloc),nValue);
}
void XMSDOS::set_e_ss(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ss),nValue);
}
void XMSDOS::set_e_sp(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_sp),nValue);
}
void XMSDOS::set_e_csum(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_csum),nValue);
}
void XMSDOS::set_e_ip(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ip),nValue);
}
void XMSDOS::set_e_cs(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cs),nValue);
}
void XMSDOS::set_e_lfarlc(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_lfarlc),nValue);
}
void XMSDOS::set_e_ovno(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ovno),nValue);
}
void XMSDOS::set_e_res(int nPosition, quint16 nValue)
{
if(nPosition<4)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_res)+sizeof(quint16)*nPosition,nValue);
}
}
void XMSDOS::set_e_oemid(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_oemid),nValue);
}
void XMSDOS::set_e_oeminfo(quint16 nValue)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_oeminfo),nValue);
}
void XMSDOS::set_e_res2(int nPosition, quint16 nValue)
{
if(nPosition<10)
{
write_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_res2)+sizeof(quint16)*nPosition,nValue);
}
}
void XMSDOS::set_e_lfanew(quint32 nValue)
{
write_uint32(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_lfanew),nValue);
}
quint16 XMSDOS::get_e_magic()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_magic));
}
quint16 XMSDOS::get_e_cblp()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cblp));
}
quint16 XMSDOS::get_e_cp()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cp));
}
quint16 XMSDOS::get_e_crlc()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_crlc));
}
quint16 XMSDOS::get_e_cparhdr()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cparhdr));
}
quint16 XMSDOS::get_e_minalloc()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_minalloc));
}
quint16 XMSDOS::get_e_maxalloc()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_maxalloc));
}
quint16 XMSDOS::get_e_ss()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ss));
}
quint16 XMSDOS::get_e_sp()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_sp));
}
quint16 XMSDOS::get_e_csum()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_csum));
}
quint16 XMSDOS::get_e_ip()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ip));
}
quint16 XMSDOS::get_e_cs()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_cs));
}
quint16 XMSDOS::get_e_lfarlc()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_lfarlc));
}
quint16 XMSDOS::get_e_ovno()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_ovno));
}
quint16 XMSDOS::get_e_res(int nPosition)
{
quint16 nResult=0;
if(nPosition<10)
{
nResult=read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_res)+sizeof(quint16)*nPosition);
}
return nResult;
}
quint16 XMSDOS::get_e_oemid()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_oemid));
}
quint16 XMSDOS::get_e_oeminfo()
{
return read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_oeminfo));
}
quint16 XMSDOS::get_e_res2(int nPosition)
{
quint16 nResult=0;
if(nPosition<10)
{
nResult=read_uint16(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_res2)+sizeof(quint16)*nPosition);
}
return nResult;
}
quint32 XMSDOS::get_e_lfanew()
{
return read_uint32(offsetof(XMSDOS_DEF::IMAGE_DOS_HEADEREX,e_lfanew));
}
XBinary::_MEMORY_MAP XMSDOS::getMemoryMap()
{
_MEMORY_MAP result={};
qint32 nIndex=0;
result.sArch=getArch();
result.sType=getTypeAsString();
result.fileType=FT_MSDOS;
result.mode=getMode();
result.nRawSize=getSize();
result.nImageSize=getImageSize();
qint64 nMaxOffset=(get_e_cp()-1)*512+get_e_cblp();
qint64 nHeaderOffset=0;
qint64 nHeaderSize=(quint16)(get_e_cparhdr()*16);
// qint64 nDataOffset=nHeaderOffset+nHeaderSize;
// qint64 nDataSize=get_e_cs()*16;
// qint64 nCodeOffset=(quint16)((quint16)(get_e_cparhdr()*16)+(quint16)(get_e_cs()*16));
qint64 nCodeOffset=((qint16)get_e_cparhdr()*16)+((qint16)get_e_cs()*16);
qint64 nCodeAddress=0;
qint64 nCodeSize=S_ALIGN_UP(nMaxOffset-nCodeOffset,512);
// nCodeSize=qMin(nCodeSize,getSize());
qint64 nOverlayOffset=nMaxOffset;
qint64 nOverlaySize=qMax(getSize()-nMaxOffset,(qint64)0);
// qint64 nBaseAddress=_getBaseAddress();
{
_MEMORY_RECORD record={};
record.sName=QString("MSDOS %1").arg(tr("Header"));
record.nSize=nHeaderSize;
record.nOffset=nHeaderOffset;
record.nAddress=-1;
record.segment=ADDRESS_SEGMENT_UNKNOWN;
record.type=MMT_HEADER;
record.nIndex=nIndex++;
result.listRecords.append(record);
}
// TODO ADDRESS_SEGMENT_DATA
if(nCodeOffset<0) // Virtual
{
qint64 nDelta=nCodeOffset-nHeaderSize;
_MEMORY_RECORD record={};
record.bIsVirtual=true;
record.nSize=qAbs(nDelta);
record.nOffset=-1;
record.nAddress=0;
record.segment=ADDRESS_SEGMENT_CODE; // CODE
record.type=MMT_LOADSECTION;
record.nIndex=nIndex++;
result.listRecords.append(record);
nCodeSize-=qAbs(nDelta);
nCodeOffset=nHeaderSize;
nCodeAddress+=qAbs(nDelta);
}
{
_MEMORY_RECORD record={};
record.nSize=nCodeSize;
record.nOffset=nCodeOffset;
record.nAddress=nCodeAddress;
record.segment=ADDRESS_SEGMENT_CODE; // CODE
record.type=MMT_LOADSECTION;
record.nIndex=nIndex++;
result.listRecords.append(record);
}
{
_MEMORY_RECORD record={};
record.bIsVirtual=true;
record.nSize=0xFFFF-nCodeSize;
record.nOffset=-1;
record.nAddress=nCodeAddress+nCodeSize;
record.segment=ADDRESS_SEGMENT_CODE; // CODE
record.type=MMT_LOADSECTION;
record.nIndex=nIndex++;
result.listRecords.append(record);
}
if(nOverlaySize)
{
_MEMORY_RECORD record={};
record.nSize=nOverlaySize;
record.nOffset=nOverlayOffset;
record.nAddress=-1;
record.sName=QString("Overlay");
record.segment=ADDRESS_SEGMENT_UNKNOWN;
record.type=MMT_OVERLAY;
record.nIndex=nIndex++;
result.listRecords.append(record);
}
return result;
}
qint64 XMSDOS::getEntryPointOffset(_MEMORY_MAP *pMemoryMap)
{
return addressToOffset(pMemoryMap,get_e_ip());
}
qint64 XMSDOS::getImageSize()
{
return 0x1000; // TODO Check
}
QMap<quint64, QString> XMSDOS::getImageMagics()
{
QMap<quint64, QString> mapResult;
mapResult.insert(0x5A4D,"IMAGE_DOS_SIGNATURE");
mapResult.insert(0x4D5A,"IMAGE_DOS_SIGNATURE_ZM");
return mapResult;
}
QMap<quint64, QString> XMSDOS::getImageMagicsS()
{
QMap<quint64, QString> mapResult;
mapResult.insert(0x5A4D,"DOS_SIGNATURE");
mapResult.insert(0x4D5A,"DOS_SIGNATURE_ZM");
return mapResult;
}
bool XMSDOS::isLE()
{
quint32 nOffset=get_e_lfanew();
quint16 nNew=read_uint16(nOffset);
return (nNew==0x454C); // TODO const
}
bool XMSDOS::isLX()
{
quint32 nOffset=get_e_lfanew();
quint16 nNew=read_uint16(nOffset);
return (nNew==0x584C); // TODO const
}
bool XMSDOS::isNE()
{
quint32 nOffset=get_e_lfanew();
quint16 nNew=read_uint16(nOffset);
return (nNew==0x454E); // TODO const
}
bool XMSDOS::isPE()
{
quint32 nOffset=get_e_lfanew();
quint16 nNew=read_uint16(nOffset);
return (nNew==0x4550); // TODO const
}
bool XMSDOS::isRichSignaturePresent()
{
bool bResult=false;
int nOffset=sizeof(XMSDOS_DEF::IMAGE_DOS_HEADER);
int nSize=get_lfanew()-sizeof(XMSDOS_DEF::IMAGE_DOS_HEADER);
if((nSize>0)&&(nSize<=0x200))
{
QByteArray baStub=read_array(nOffset,nSize);
bResult=baStub.contains("Rich");
}
return bResult;
}
QList<XMSDOS::MS_RICH_RECORD> XMSDOS::getRichSignatureRecords()
{
QList<MS_RICH_RECORD> listResult;
qint64 nOffset=find_ansiString(getDosStubOffset(),getDosStubSize(),"Rich");
if(nOffset!=-1)
{
quint32 nXORkey=read_uint32(nOffset+4);
qint64 nCurrentOffset=nOffset-4;
while(nCurrentOffset>getDosStubOffset()) // TODO optimize
{
quint32 nTemp=read_uint32(nCurrentOffset)^nXORkey;
if(nTemp==0x536e6144) // DanS
{
nCurrentOffset+=16;
for(; nCurrentOffset<nOffset; nCurrentOffset+=8)
{
MS_RICH_RECORD record={};
quint32 nValue1=read_uint32(nCurrentOffset)^nXORkey;
record.nId=nValue1>>16;
record.nVersion=nValue1&0xFFFF;
quint32 nValue2=read_uint32(nCurrentOffset+4)^nXORkey;
record.nCount=nValue2;
listResult.append(record);
}
break;
}
nCurrentOffset-=4;
}
}
return listResult;
}
qint32 XMSDOS::getNumberOfRichIDs()
{
QList<MS_RICH_RECORD> listRecords=getRichSignatureRecords();
return getNumberOfRichIDs(&listRecords);
}
qint32 XMSDOS::getNumberOfRichIDs(QList<XMSDOS::MS_RICH_RECORD> *pListRich)
{
return pListRich->count();
}
bool XMSDOS::isRichVersionPresent(quint32 nVersion)
{
QList<MS_RICH_RECORD> listRecords=getRichSignatureRecords();
return isRichVersionPresent(nVersion,&listRecords);
}
bool XMSDOS::isRichVersionPresent(quint32 nVersion, QList<XMSDOS::MS_RICH_RECORD> *pListRich)
{
bool bResult=false;
int nCount=pListRich->count();
for(int i=0;i<nCount;i++)
{
if(pListRich->at(i).nVersion==nVersion)
{
bResult=true;
break;
}
}
return bResult;
}
qint64 XMSDOS::getDosStubSize()
{
qint64 nSize=(qint64)get_lfanew()-sizeof(XMSDOS_DEF::IMAGE_DOS_HEADEREX);
nSize=qMax(nSize,(qint64)0);
return nSize;
}
qint64 XMSDOS::getDosStubOffset()
{
return sizeof(XMSDOS_DEF::IMAGE_DOS_HEADEREX);
}
QByteArray XMSDOS::getDosStub()
{
return read_array(getDosStubOffset(),getDosStubSize());
}
bool XMSDOS::isDosStubPresent()
{
return getDosStubSize()!=0;
}
XBinary::MODE XMSDOS::getMode()
{
return MODE_16;
}
QString XMSDOS::getArch()
{
return QString("8086");
}
bool XMSDOS::isBigEndian()
{
return false;
}
int XMSDOS::getType()
{
return TYPE_EXE;
}
QString XMSDOS::typeIdToString(int nType)
{
QString sResult="Unknown"; // mb TODO translate
switch(nType)
{
case TYPE_UNKNOWN: sResult=QString("Unknown"); break; // mb TODO translate
case TYPE_EXE: sResult=QString("EXE"); break;
}
return sResult;
}