-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
3056 lines (2788 loc) · 106 KB
/
Program.cs
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Disassembler;
using Disassembler.CPU;
using Disassembler.Decompiler;
using Disassembler.MZ;
using Disassembler.NE;
using Disassembler.OMF;
using IRB.Collections.Generic;
using System.Data.Common;
using System.Reflection;
using System.Reflection.Metadata;
internal class Program
{
private static void Main(string[] args)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Out\\";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
if (!Directory.Exists(path+"Code"))
Directory.CreateDirectory(path+"Code");
if (!Directory.Exists(path + "Data"))
Directory.CreateDirectory(path + "Data");
//MakeTable();
//UnpackDOSEXE(@"..\..\..\..\Game\Dos\Installed\civ.bak");
ParseDOSEXE(path);
//ParseWinEXE(path);
}
private static void UnpackDOSEXE(string path)
{
MZExecutable mzEXE = new MZExecutable(path);
ushort usSegment1 = 0x5409;
ushort usSegment2 = 0x1234;
CPURegisters oRegisters1 = new CPURegisters();
Memory oMemory1 = UnpackEXE(mzEXE, usSegment1, oRegisters1);
CPURegisters oRegisters2 = new CPURegisters();
Memory oMemory2 = UnpackEXE(mzEXE, usSegment2, oRegisters2);
byte[] buffer1 = oMemory1.Blocks[3].Data;
byte[] buffer2 = oMemory2.Blocks[3].Data;
int iLength1 = buffer1.Length;
int iLength2 = buffer1.Length;
uint uiLastEmptyByte = (uint)(iLength1 - 1);
if (iLength1 != iLength2)
throw new Exception("Blocks are of different size");
for (int i = iLength1 - 1; i >= 0; i--)
{
if (buffer1[i] != 0 || buffer2[i] != 0)
{
uiLastEmptyByte = (uint)(i + 1);
break;
}
}
Console.WriteLine("Last empty byte in the last block: 0x{0:x8}", uiLastEmptyByte);
MemoryRegion.AlignBlock(ref uiLastEmptyByte); // we want this to be aligned
Console.WriteLine("Joining the blocks");
iLength1 = oMemory1.Blocks[2].Data.Length;
iLength2 = (int)(oMemory2.Blocks[2].Data.Length + uiLastEmptyByte);
oMemory1.Blocks[2].Resize(iLength2);
oMemory2.Blocks[2].Resize(iLength2);
Array.Copy(oMemory1.Blocks[3].Data, 0, oMemory1.Blocks[2].Data, iLength1, uiLastEmptyByte);
oMemory1.Blocks.RemoveAt(3);
Array.Copy(oMemory2.Blocks[3].Data, 0, oMemory2.Blocks[2].Data, iLength1, uiLastEmptyByte);
oMemory2.Blocks.RemoveAt(3);
buffer1 = oMemory1.Blocks[2].Data;
buffer2 = oMemory2.Blocks[2].Data;
/*Console.WriteLine("Writing blocks to files");
FileStream writer = new FileStream("exe1.bin", FileMode.Create);
writer.Write(buffer1, 0, buffer1.Length);
writer.Close();
writer = new FileStream("exe2.bin", FileMode.Create);
writer.Write(buffer2, 0, buffer2.Length);
writer.Close();*/
// decrease minimum allocation by additional size that was added to EXE
mzEXE.MinimumAllocation -= (ushort)(uiLastEmptyByte >> 4);
CompareBlocksAndReconstructEXE(mzEXE, buffer1, buffer2, usSegment1, usSegment2);
mzEXE.InitialSS = (ushort)(oRegisters1.SS.Word - usSegment1);
mzEXE.InitialSP = oRegisters1.SP.Word;
mzEXE.InitialIP = oRegisters1.IP.Word;
mzEXE.InitialCS = (ushort)(oRegisters1.CS.Word - usSegment1); // - 0x10; // account for PSP (0x10)
Console.WriteLine("Block validation passed");
// write new EXE
mzEXE.WriteToFile("civ.exe");
}
private static void ParseDOSEXE(string path)
{
ushort usSegmentOffset = 0x1000;
MZExecutable mzEXE = new MZExecutable(@"..\..\..\..\..\Game\Dos\Installed\civ.exe");
mzEXE.ApplyRelocations(usSegmentOffset);
Library oLibrary1 = new Library(@"..\..\..\..\..\Compilers\MSC\Installed\MSC\LIB\MLIBC7.lib");
// unique set of segment names
BHashSet<string> aSegmentNames = new BHashSet<string>();
BHashSet<string> aGroupNames = new BHashSet<string>();
for (int i = 0; i < oLibrary1.Modules.Count; i++)
{
OBJModule module = oLibrary1.Modules[i];
for (int j = 0; j < module.DataRecords.Count; j++)
{
DataRecord data = module.DataRecords[j];
aSegmentNames.Add(data.Segment.Name);
}
for (int j = 0; j < module.SegmentGroups.Count; j++)
{
SegmentGroupDefinition group = module.SegmentGroups[j];
aGroupNames.Add(group.Name);
}
}
Console.WriteLine("Used segment names:");
for (int i = 0; i < aSegmentNames.Count; i++)
{
Console.WriteLine(aSegmentNames[i]);
}
Console.WriteLine("Group names:");
for (int i = 0; i < aGroupNames.Count; i++)
{
Console.WriteLine(aGroupNames[i]);
}
List<ModuleMatch> aMatches = new List<ModuleMatch>();
Console.WriteLine("Matching MLIBC7");
MatchLibraryToEXE(oLibrary1, mzEXE, aMatches);
MZDecompiler oDecompiler = new MZDecompiler(mzEXE, aMatches);
Console.WriteLine("Decompiling");
// we start by decompiling Start function
oDecompiler.Decompile("Start", CallTypeEnum.Undefined, new List<CParameter>(), CType.Word,
0, (ushort)(mzEXE.InitialCS + usSegmentOffset), mzEXE.InitialIP, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x2fa1:x4}_{0x644:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x2fa1, 0x644, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1000:x4}_{0x1a7:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1000, 0x1a7, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x3045:x4}_{0x2b44:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x3045, 0x2b44, (uint)usSegmentOffset << 4);
// Segment 0x1866
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1169:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1169, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1280:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1280, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x135a:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x135a, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x13a9:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x13a9, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x13f8:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x13f8, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x14a2:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x14a2, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x14f6:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x14f6, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1560:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1560, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1593:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1593, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1610:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1610, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1643:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1643, (uint)usSegmentOffset << 4);
oDecompiler.Decompile($"F0_{0x1866:x4}_{0x1676:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void,
0, 0x1866, 0x1676, (uint)usSegmentOffset << 4);
// emit generated code
StreamWriter writer = new StreamWriter("Out\\Code\\Objects.cs");
StreamWriter writer1 = new StreamWriter("Out\\Code\\Inits.cs");
StreamWriter writer2 = new StreamWriter("Out\\Code\\Getters.cs");
// emit segments
BDictionary<int, List<MZFunction>> oSegments = new BDictionary<int, List<MZFunction>>();
for (int i = 0; i < oDecompiler.GlobalNamespace.Functions.Count; i++)
{
MZFunction function = oDecompiler.GlobalNamespace.Functions[i].Value;
if (function.Overlay == 0)
{
if (oSegments.ContainsKey(function.Segment))
{
oSegments.GetValueByKey(function.Segment).Add(function);
}
else
{
oSegments.Add(function.Segment, new List<MZFunction>());
oSegments.GetValueByKey(function.Segment).Add(function);
}
}
}
for (int i = 0; i < oSegments.Count; i++)
{
oDecompiler.WriteCode($"Out\\Code\\Segment_{oSegments[i].Key:x}.cs", oSegments[i].Value);
writer.WriteLine($"private Segment_{oSegments[i].Key:x} oSegment_{oSegments[i].Key:x};");
writer1.WriteLine($"this.oSegment_{oSegments[i].Key:x} = new Segment_{oSegments[i].Key:x}(this);");
writer2.WriteLine($"public Segment_{oSegments[i].Key:x} Segment_{oSegments[i].Key:x}");
writer2.WriteLine("{");
writer2.WriteLine($"\tget {{ return this.oSegment_{oSegments[i].Key:x};}}");
writer2.WriteLine("}");
writer2.WriteLine();
}
// emit overlays
oSegments.Clear();
for (int i = 0; i < oDecompiler.GlobalNamespace.Functions.Count; i++)
{
MZFunction function = oDecompiler.GlobalNamespace.Functions[i].Value;
if (function.Overlay > 0)
{
if (oSegments.ContainsKey(function.Overlay))
{
oSegments.GetValueByKey(function.Overlay).Add(function);
}
else
{
oSegments.Add(function.Overlay, new List<MZFunction>());
oSegments.GetValueByKey(function.Overlay).Add(function);
}
}
}
for (int i = 0; i < oSegments.Count; i++)
{
oDecompiler.WriteCode($"Out\\Code\\Overlay_{oSegments[i].Key}.cs", oSegments[i].Value);
writer.WriteLine($"private Overlay_{oSegments[i].Key} oOverlay_{oSegments[i].Key};");
writer1.WriteLine($"this.oOverlay_{oSegments[i].Key} = new Overlay_{oSegments[i].Key}(this);");
writer2.WriteLine($"public Overlay_{oSegments[i].Key} Overlay_{oSegments[i].Key}");
writer2.WriteLine("{");
writer2.WriteLine($"\tget {{ return this.oOverlay_{oSegments[i].Key};}}");
writer2.WriteLine("}");
writer2.WriteLine();
}
int iMaxOverlaySize = 0;
for (int i = 0; i < mzEXE.Overlays.Count; i++)
{
iMaxOverlaySize = Math.Max(iMaxOverlaySize, mzEXE.Overlays[i].Data.Length);
}
Console.WriteLine($"Maximum overlay size in bytes: 0x{iMaxOverlaySize:x4}");
// emit API functions
List <MZFunction> aFunctions = new List<MZFunction>();
for (int i = 0; i < oDecompiler.GlobalNamespace.APIFunctions.Count; i++)
{
aFunctions.Add(oDecompiler.GlobalNamespace.APIFunctions[i].Value);
}
oDecompiler.WriteCode(@"Out\Code\MSCAPI.cs", aFunctions);
writer.WriteLine("private MSCAPI oMSCAPI;");
writer1.WriteLine("this.oMSCAPI = new MSCAPI(this);");
writer2.WriteLine("public MSCAPI MSCAPI");
writer2.WriteLine("{");
writer2.WriteLine("\tget { return this.oMSCAPI;}");
writer2.WriteLine("}");
// process misc.exe
Console.WriteLine("Processing overlay Misc");
MZExecutable miscEXE = new MZExecutable(@"..\..\..\..\..\Game\Dos\Installed\misc.exe");
MZDecompiler oMiscDecompiler = new MZDecompiler(miscEXE, aMatches);
oMiscDecompiler.DecompileOverlay();
// Emit Misc functions
aFunctions = new List<MZFunction>();
for (int i = 0; i < oMiscDecompiler.GlobalNamespace.Functions.Count; i++)
{
aFunctions.Add(oMiscDecompiler.GlobalNamespace.Functions[i].Value);
}
oMiscDecompiler.WriteCode(@"Out\Code\Misc.cs", aFunctions);
writer.WriteLine("private Misc oMisc;");
writer1.WriteLine("this.oMisc = new Misc(this);");
writer2.WriteLine();
writer2.WriteLine("public Misc Misc");
writer2.WriteLine("{");
writer2.WriteLine("\tget { return this.oMisc;}");
writer2.WriteLine("}");
Console.WriteLine("Processing overlay VGA");
MZExecutable vgaEXE = new MZExecutable(@"..\..\..\..\..\Game\Dos\Installed\mgraphic.exe");
MZDecompiler oVGADecompiler = new MZDecompiler(vgaEXE, aMatches);
oVGADecompiler.DecompileOverlay();
/*oEGADecompiler.Decompile($"F0_0000_{0x19b8:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void, 0, 0, 0x19b8, 0);
oEGADecompiler.Decompile($"F0_0000_{0x1a08:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void, 0, 0, 0x1a08, 0);
oEGADecompiler.Decompile($"F0_0000_{0x1a3c:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void, 0, 0, 0x1a3c, 0);
oEGADecompiler.Decompile($"F0_0000_{0xcf1:x4}", CallTypeEnum.Undefined, new List<CParameter>(), CType.Void, 0, 0, 0xcf1, 0);*/
// Emit egraphic functions
aFunctions = new List<MZFunction>();
for (int i = 0; i < oVGADecompiler.GlobalNamespace.Functions.Count; i++)
{
aFunctions.Add(oVGADecompiler.GlobalNamespace.Functions[i].Value);
}
oVGADecompiler.WriteCode(@"Out\Code\VGADriver.cs", aFunctions);
writer.WriteLine("private VGADriver oVGA;");
writer1.WriteLine("this.oVGA = new VGADriver(this);");
writer2.WriteLine();
writer2.WriteLine("public VGADriver VGA");
writer2.WriteLine("{");
writer2.WriteLine("\tget { return this.oVGA;}");
writer2.WriteLine("}");
Console.WriteLine("Processing overlay NSound");
MZExecutable nsEXE = new MZExecutable(@"..\..\..\..\..\Game\Dos\Installed\nsound.cvl");
MZDecompiler oNSecompiler = new MZDecompiler(nsEXE, aMatches);
oNSecompiler.DecompileOverlay();
// Emit egraphic functions
aFunctions = new List<MZFunction>();
for (int i = 0; i < oNSecompiler.GlobalNamespace.Functions.Count; i++)
{
aFunctions.Add(oNSecompiler.GlobalNamespace.Functions[i].Value);
}
oNSecompiler.WriteCode(@"Out\Code\NSound.cs", aFunctions);
writer.WriteLine("private NSound oNSound;");
writer1.WriteLine("this.oNSound = new NSound(this);");
writer2.WriteLine();
writer2.WriteLine("public NSound NSound");
writer2.WriteLine("{");
writer2.WriteLine("\tget { return this.oNSound;}");
writer2.WriteLine("}");
writer2.Close();
writer1.Close();
writer.Close();
Console.WriteLine("Finished DOS processing");
}
private static void CompareBlocksAndReconstructEXE(MZExecutable exe, byte[] block1, byte[] block2, ushort segment1, ushort segment2)
{
if (block1.Length != block2.Length)
{
Console.WriteLine("Block 1 length is not equal to Block 2 length");
return;
}
// also, reconstruct relocation items
int iSegment = 0;
int iOffset = 0;
exe.Relocations.Clear();
// we are comparing words
for (int i = 0; i < block1.Length; i++)
{
if (block1[i] != block2[i])
{
if (block1[i + 1] != block2[i + 1])
{
// compare absolute offsets, not relative ones
ushort usWord1 = (ushort)((ushort)block1[i] | (ushort)((ushort)block1[i + 1] << 8));
usWord1 -= segment1;
//usWord1 -= 0x10; // account for PSP (0x10)
ushort usWord2 = (ushort)((ushort)block2[i] | (ushort)((ushort)block2[i + 1] << 8));
usWord2 -= segment2;
//usWord2 -= 0x10; // account for PSP (0x10)
if (usWord1 != usWord2)
{
Console.WriteLine("Segment 0x{0:x4} not equal to 0x{1:x4} at 0x{2:x4}", usWord1, usWord2, i);
}
block1[i] = (byte)(usWord1 & 0xff);
block1[i + 1] = (byte)((usWord1 & 0xff00) >> 8);
block2[i] = (byte)(usWord2 & 0xff);
block2[i + 1] = (byte)((usWord2 & 0xff00) >> 8);
exe.Relocations.Add(new MZRelocationItem((ushort)iSegment, (ushort)iOffset));
iOffset++;
i++;
}
else
{
Console.WriteLine("Block 1 word is not equal in size to Block 2 at 0x{0:x4}", i);
return;
}
}
iOffset++;
if (iOffset > 0xffff)
{
iSegment += 0x1000;
iOffset -= 0x10000;
}
}
exe.Data = new byte[block1.Length];
Array.Copy(block1, exe.Data, block1.Length);
}
private static Memory UnpackEXE(MZExecutable exe, ushort startSegment, CPURegisters r)
{
Memory oMemory = new Memory();
ushort usPSPSegment;
ushort usMZSegment;
ushort usDataSegment;
uint uiMZEXELength = (uint)exe.Data.Length;
MemoryRegion.AlignBlock(ref uiMZEXELength);
if (startSegment < 0x10)
throw new Exception("starting segment must be greater than 0x10");
// blank start segment
oMemory.AllocateParagraphs((ushort)(startSegment - 0x10), out usPSPSegment);
oMemory.MemoryRegions.Add(new MemoryRegion(0, (startSegment - 0x10) << 4, MemoryFlagsEnum.None));
// PSP segment
oMemory.AllocateParagraphs(0x10, out usPSPSegment);
oMemory.MemoryRegions.Add(new MemoryRegion(usPSPSegment, 0x100, MemoryFlagsEnum.None));
// EXE segment
oMemory.AllocateBlock((int)uiMZEXELength, out usMZSegment);
oMemory.WriteBlock(usMZSegment, 0, exe.Data, 0, exe.Data.Length);
// data segment
oMemory.AllocateParagraphs((ushort)exe.MinimumAllocation, out usDataSegment);
// decode EXE packer
r.SP.Word = (ushort)exe.InitialSP;
r.DS.Word = usPSPSegment;
r.ES.Word = usPSPSegment;
r.SS.Word = (ushort)(exe.InitialSS + usMZSegment);
r.CS.Word = (ushort)(exe.InitialCS + usMZSegment);
bool bDFlag = false;
// Decoding phase 1
// 2b0d:0010 mov ax, es
r.AX.Word = r.ES.Word;
// 2b0d:0012 add ax, 10h
r.AX.Word += 0x10;
// 2b0d:0015 push cs; pop ds
r.DS.Word = r.CS.Word;
// 2b0d:0017 mov word_2B0D_4, ax
oMemory.WriteWord(r.DS.Word, 0x4, r.AX.Word);
// 2b0d:001A add ax, word_2B0D_C
r.AX.Word += oMemory.ReadWord(r.DS.Word, 0xc);
// 2b0d:001E mov es, ax
r.ES.Word = r.AX.Word;
// 2b0d:0020 mov cx, word_2B0D_6
r.CX.Word = oMemory.ReadWord(r.DS.Word, 0x6);
// 2b0d:0024 mov di, cx
r.DI.Word = r.CX.Word;
// 2b0d:0026 dec di
r.DI.Word--;
// 2b0d:0027 mov si, di
r.SI.Word = r.DI.Word;
// 2b0d:0029 std
bDFlag = true;
// 2b0d:002A rep movsb
while (r.CX.Word != 0)
{
oMemory.WriteByte(r.ES.Word, r.DI.Word, oMemory.ReadByte(r.DS.Word, r.SI.Word));
if (bDFlag)
{
r.DI.Word--;
r.SI.Word--;
}
else
{
r.DI.Word++;
r.SI.Word++;
}
r.CX.Word--;
}
// 2b0d:002C push ax
// 2b0d:002D mov ax, 32h; push ax
// 2b0d:0031 retf
//oMemory.WriteWord(r.SS.Word, (ushort)(r.SP.Word - 2), r.AX.Word);
//oMemory.WriteWord(r.SS.Word, (ushort)(r.SP.Word - 4), 0x32);
r.CS.Word = r.AX.Word;
r.IP.Word = 0x32;
//oMemory.MemoryRegions.Add(new MemoryRegion(r.AX.Word, 0x32, 0x10f - 0x32, MemoryFlagsEnum.Read));
//Console.WriteLine("Protecting block at 0x{0:x8}, size 0x{1:x4}", MemoryRegion.ToAbsolute(r.AX.Word, 0x32), 0x10f - 0x32);
// Decoding phase 2
// CS:IP = AX:0x32
// 0x0032 MOV BX, ES
r.BX.Word = r.ES.Word;
// 0x0034 MOV AX, DS
r.AX.Word = r.DS.Word;
// 0x0036 DEC AX
r.AX.Word--;
// 0x0037 MOV DS, AX
r.DS.Word = r.AX.Word;
// 0x0039 MOV ES, AX
r.ES.Word = r.AX.Word;
// 0x003b MOV DI, 0xf
r.DI.Word = 0xf;
// 0x003e MOV CX, 0x10
r.CX.Word = 0x10;
// 0x0041 MOV AL, 0xff
r.AX.Low = 0xff;
// 0x0043 REPE SCASB
while (r.CX.Word != 0)
{
byte res = (byte)(((int)r.AX.Low - (int)oMemory.ReadByte(r.ES.Word, r.DI.Word)) & 0xff);
if (bDFlag)
{
r.DI.Word--;
}
else
{
r.DI.Word++;
}
r.CX.Word--;
if (res != 0)
break;
}
// 0x0045 INC DI
r.DI.Word++;
// 0x0046 MOV SI, DI
r.SI.Word = r.DI.Word;
// 0x0048 MOV AX, BX
r.AX.Word = r.BX.Word;
// 0x004a DEC AX
r.AX.Word--;
// 0x004b MOV ES, AX
r.ES.Word = r.AX.Word;
// 0x004d MOV DI, 0xf
r.DI.Word = 0xf;
l50:
// 0x0050 MOV CL, 0x4
r.CX.Low = 0x4;
// 0x0052 MOV AX, SI
r.AX.Word = r.SI.Word;
// 0x0054 NOT AX
r.AX.Word = (ushort)(~r.AX.Word);
// 0x0056 SHR AX, CL
r.AX.Word = (ushort)(r.AX.Word >> r.CX.Low);
// 0x0058 JE + 0x9
if (r.AX.Word == 0) goto l63;
// 0x005a MOV DX, DS
r.DX.Word = r.DS.Word;
// 0x005c SUB DX, AX
r.DX.Word -= r.AX.Word;
// 0x005e MOV DS, DX
r.DS.Word = r.DX.Word;
// 0x0060 OR SI, 0xfff0
r.SI.Word |= 0xfff0;
l63:
// 0x0063 MOV AX, DI
r.AX.Word = r.DI.Word;
// 0x0065 NOT AX
r.AX.Word = (ushort)(~r.AX.Word);
// 0x0067 SHR AX, CL
r.AX.Word = (ushort)(r.AX.Word >> r.CX.Low);
// 0x0069 JE + 0x9
if (r.AX.Word == 0) goto l74;
// 0x006b MOV DX, ES
r.DX.Word = r.ES.Word;
// 0x006d SUB DX, AX
r.DX.Word -= r.AX.Word;
// 0x006f MOV ES, DX
r.ES.Word = r.DX.Word;
// 0x0071 OR DI, 0xfff0
r.DI.Word |= 0xfff0;
l74:
// 0x0074 LODSB
r.AX.Low = oMemory.ReadByte(r.DS.Word, r.SI.Word);
if (bDFlag)
{
r.SI.Word--;
}
else
{
r.SI.Word++;
}
// 0x0075 MOV DL, AL
r.DX.Low = r.AX.Low;
// 0x0077 DEC SI
r.SI.Word--;
// 0x0078 LODSW
r.AX.Word = oMemory.ReadWord(r.DS.Word, r.SI.Word);
if (bDFlag)
{
r.SI.Word -= 2;
}
else
{
r.SI.Word += 2;
}
// 0x0079 MOV CX, AX
r.CX.Word = r.AX.Word;
// 0x007b INC SI
r.SI.Word++;
// 0x007c MOV AL, DL
r.AX.Low = r.DX.Low;
// 0x007e AND AL, 0xfe
r.AX.Low &= 0xfe;
// 0x0080 CMP AL, 0xb0
// 0x0082 JNE + 0x6
if (r.AX.Low != 0xb0) goto l8a;
// 0x0084 LODSB
r.AX.Low = oMemory.ReadByte(r.DS.Word, r.SI.Word);
if (bDFlag)
{
r.SI.Word--;
}
else
{
r.SI.Word++;
}
// 0x0085 REPE STOSB
while (r.CX.Word != 0)
{
oMemory.WriteByte(r.ES.Word, r.DI.Word, r.AX.Low);
if (bDFlag)
{
r.DI.Word--;
}
else
{
r.DI.Word++;
}
r.CX.Word--;
}
// 0x0087 JMP + 0x7
goto l90;
// 0x0089 NOP
l8a:
// 0x008a CMP AL, 0xb2
// 0x008c JNE + 0x6b
if (r.AX.Low != 0xb2) goto lf9;
// 0x008e REPE MOVSB
while (r.CX.Word != 0)
{
oMemory.WriteByte(r.ES.Word, r.DI.Word, oMemory.ReadByte(r.DS.Word, r.SI.Word));
if (bDFlag)
{
r.DI.Word--;
r.SI.Word--;
}
else
{
r.DI.Word++;
r.SI.Word++;
}
r.CX.Word--;
}
l90:
// 0x0090 MOV AL, DL
r.AX.Low = r.DX.Low;
// 0x0092 TEST AL, 0x1
// 0x0094 JE - 0x46
if ((r.AX.Low & 1) == 0) goto l50;
// 0x0096 MOV SI, 0x125
r.SI.Word = 0x125;
// 0x0099 PUSH CS
// 0x009a POP DS
r.DS.Word = r.CS.Word;
// 0x009b MOV BX, [0x4]
r.BX.Word = oMemory.ReadWord(r.DS.Word, 0x4);
// 0x009f CLD
bDFlag = false;
// 0x00a0 XOR DX, DX
r.DX.Word = 0;
la2:
// 0x00a2 LODSW
r.AX.Word = oMemory.ReadWord(r.DS.Word, r.SI.Word);
if (bDFlag)
{
r.SI.Word -= 2;
}
else
{
r.SI.Word += 2;
}
// 0x00a3 MOV CX, AX
r.CX.Word = r.AX.Word;
// 0x00a5 JCXZ + 0x13
if (r.CX.Word == 0) goto lba;
// 0x00a7 MOV AX, DX
r.AX.Word = r.DX.Word;
// 0x00a9 ADD AX, BX
r.AX.Word += r.BX.Word;
// 0x00ab MOV ES, AX
r.ES.Word = r.AX.Word;
lad:
// 0x00ad LODSW
r.AX.Word = oMemory.ReadWord(r.DS.Word, r.SI.Word);
if (bDFlag)
{
r.SI.Word -= 2;
}
else
{
r.SI.Word += 2;
}
// 0x00ae MOV DI, AX
r.DI.Word = r.AX.Word;
// 0x00b0 CMP DI, 0xffff
// 0x00b3 JE + 0x11
if (r.DI.Word == 0xffff) goto lc6;
// 0x00b5 ADD ES:[DI], BX
oMemory.WriteWord(r.ES.Word, r.DI.Word, (ushort)(oMemory.ReadWord(r.ES.Word, r.DI.Word) + r.BX.Word));
lb8:
// 0x00b8 LOOP - 0xd
r.CX.Word--;
if (r.CX.Word != 0) goto lad;
lba:
// 0x00ba CMP DX, 0xf000
// 0x00be JE + 0x16
if (r.DX.Word == 0xf000) goto ld6;
// 0x00c0 ADD DX, 0x1000
r.DX.Word += 0x1000;
// 0x00c4 JMP - 0x24
goto la2;
lc6:
// 0x00c6 MOV AX, ES
r.AX.Word = r.ES.Word;
// 0x00c8 INC AX
r.AX.Word++;
// 0x00c9 MOV ES, AX
r.ES.Word = r.AX.Word;
// 0x00cb SUB DI, 0x10
r.DI.Word -= 0x10;
// 0x00ce ADD ES:[DI], BX
oMemory.WriteWord(r.ES.Word, r.DI.Word, (ushort)(oMemory.ReadWord(r.ES.Word, r.DI.Word) + r.BX.Word));
// 0x00d1 DEC AX
r.AX.Word--;
// 0x00d2 MOV ES, AX
r.ES.Word = r.AX.Word;
// 0x00d4 JMP - 0x1e
goto lb8;
ld6:
// 0x00d6 MOV AX, BX
r.AX.Word = r.BX.Word;
// 0x00d8 MOV DI, [0x8]
r.DI.Word = oMemory.ReadWord(r.DS.Word, 0x8);
// 0x00dc MOV SI, [0xa]
r.SI.Word = oMemory.ReadWord(r.DS.Word, 0xa);
// 0x00e0 ADD SI, AX
r.SI.Word += r.AX.Word;
// 0x00e2 ADD [0x2], AX
oMemory.WriteWord(r.DS.Word, 0x2, (ushort)(oMemory.ReadWord(r.DS.Word, 0x2) + r.AX.Word));
// 0x00e6 SUB AX, 0x10
r.AX.Word -= 0x10;
// 0x00e9 MOV DS, AX
r.DS.Word = r.AX.Word;
// 0x00eb MOV ES, AX
r.ES.Word = r.AX.Word;
// 0x00ed MOV BX, 0x0
r.BX.Word = 0;
// 0x00f0 CLI
// 0x00f1 MOV SS, SI
r.SS.Word = r.SI.Word;
// 0x00f3 MOV SP, DI
r.SP.Word = r.DI.Word;
// 0x00f5 STI
// 0x00f6 JMP far CS:[BX]
r.IP.Word = oMemory.ReadWord(r.CS.Word, r.BX.Word);
r.CS.Word = oMemory.ReadWord(r.CS.Word, (ushort)(r.BX.Word + 2));
goto finished;
lf9:
throw new Exception("Error decoding file");
finished:
return oMemory;
}
private static void ParseWinEXE(string path)
{
// load libraries and modules
OBJModule oModule = new OBJModule(@"..\..\..\..\Compilers\Borland\Installed1\BORLANDC\LIB\C0WL.obj");
Library oLibrary1 = new Library(@"..\..\..\..\Compilers\Borland\Installed1\BORLANDC\LIB\CWL.lib");
Library oLibrary2 = new Library(@"..\..\..\..\Compilers\Borland\Installed1\BORLANDC\LIB\MATHWL.lib");
NewExecutable exe = new NewExecutable(@"..\..\..\..\Game\Win\Installed\civ.exe");
//NewExecutable exe = new NewExecutable(@"C:\Users\rajko\Documents\Projects\Disassembler\Win\Installed\CIVWIN\civ.exe");
exe.ApplyRelocations();
//Console.WriteLine("0x{0:x8}", exe.CSIP);
/*for (int i = 0; i < exe.Resources.Count; i++)
{
NEResourceTypeContainer resourceType = exe.Resources[i];
switch (resourceType.Type)
{
case ResourceTypeEnum.RT_CURSOR:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Cursor_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Cursor_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_BITMAP:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Bitmap_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Bitmap_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_ICON:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Icon_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Icon_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_MENU:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Menu_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Menu_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_DIALOG:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Dialog_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Dialog_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_STRING:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\String_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\String_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_FONTDIR:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\FontDir_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\FontDir_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_FONT:
for (int j = 0; j < resourceType.Resources.Count; j++)
{
NEResource resource = resourceType.Resources[j];
FileStream rscWriter;
if (!string.IsNullOrEmpty(resource.Name))
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Font_{1:x4}_{2}.bin",
path, resource.ID, resource.Name), FileMode.Create);
}
else
{
rscWriter = new FileStream(string.Format("{0}\\Resources\\Font_{1:x4}.bin",
path, resource.ID), FileMode.Create);
}
rscWriter.Write(resource.Data, 0, resource.Data.Length);
rscWriter.Flush();
rscWriter.Close();
}
break;
case ResourceTypeEnum.RT_ACCELERATOR:
for (int j = 0; j < resourceType.Resources.Count; j++)
{