home *** CD-ROM | disk | FTP | other *** search
/ Guide To Cracking 2002 / Guide_to_Cracking_2002.iso / Programming / yPP-B-SRC.ZIP / Unit6.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-30  |  4.9 KB  |  193 lines

  1. unit Unit6;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Gauges, ExtCtrls, StdCtrls, Buttons,d32lib,pelib, ComCtrls;
  8.  
  9. type
  10.   Tcompareform = class(TForm)
  11.     Panel1: TPanel;
  12.     Gauge1: TGauge;
  13.     OpenDialog1: TOpenDialog;
  14.     Edit1: TEdit;
  15.     Edit2: TEdit;
  16.     Panel2: TPanel;
  17.     Panel3: TPanel;
  18.     SpeedButton1: TSpeedButton;
  19.     SpeedButton2: TSpeedButton;
  20.     SpeedButton3: TSpeedButton;
  21.     SpeedButton4: TSpeedButton;
  22.     procedure FormShow(Sender: TObject);
  23.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  24.     procedure SpeedButton2Click(Sender: TObject);
  25.     procedure SpeedButton3Click(Sender: TObject);
  26.     procedure SpeedButton4Click(Sender: TObject);
  27.     procedure SpeedButton1Click(Sender: TObject);
  28.   private
  29.     { Private-Deklarationen }
  30.   public
  31.     { Public-Deklarationen }
  32.   end;
  33.  
  34. var
  35.   compareform: Tcompareform;
  36.  
  37. implementation
  38.  
  39. uses Unit1;
  40.  
  41. {$R *.DFM}
  42.  
  43. function offset2va( tmp : dword ):dword;
  44. var thesection       : byte;
  45.     i                : byte;
  46. begin
  47. if tmp < peh.OptionalHeader.SizeOfHeaders then // it's in the Header
  48.   begin
  49.   result:=tmp+peh.OptionalHeader.ImageBase;
  50.   exit;
  51.   end;
  52. // it's in any section...I hope
  53. sectionh2:=PIMAGE_SECTION_HEADER(peh);
  54. inc(pimage_nt_headers(sectionh2));
  55. inc(sectionh2);
  56. // in which section is tmp ?
  57. thesection:=1;
  58. for i:=2 to peh.FileHeader.NumberOfSections do
  59.   begin
  60.   if (tmp >= sectionh2.PointerToRawData) then thesection:=i;
  61.   inc(sectionh2);
  62.   end;
  63. // calculate the VA
  64. sectionh2:=PIMAGE_SECTION_HEADER(peh);
  65. inc(pimage_nt_headers(sectionh2));
  66. for i:=1 to thesection-1 do
  67.   inc(sectionh2);
  68. tmpnum:=sectionh2.VirtualAddress-sectionh2.PointerToRawData;
  69. result:=tmp+tmpnum+peh.OptionalHeader.ImageBase;
  70. end;
  71.  
  72. procedure Tcompareform.FormShow(Sender: TObject);
  73. begin
  74. edit1.text:='';
  75. edit2.text:='';
  76. gauge1.progress:=0;
  77. form1.enabled:=false;
  78. end;
  79.  
  80. procedure Tcompareform.FormClose(Sender: TObject;
  81.   var Action: TCloseAction);
  82. begin
  83. form1.enabled:=true;
  84. end;
  85.  
  86. procedure Tcompareform.SpeedButton2Click(Sender: TObject);
  87. begin
  88. close;
  89. end;
  90.  
  91. procedure Tcompareform.SpeedButton3Click(Sender: TObject);
  92. begin
  93. opendialog1.filename:='';
  94. opendialog1.Title:='Choose the Original File...';
  95. if opendialog1.execute then
  96.   edit1.text:=opendialog1.FileName;
  97. end;
  98.  
  99. procedure Tcompareform.SpeedButton4Click(Sender: TObject);
  100. begin
  101. opendialog1.filename:='';
  102. opendialog1.Title:='Choose the Modified File...';
  103. if opendialog1.execute then
  104.   edit2.text:=opendialog1.FileName;
  105. end;
  106.  
  107. procedure Tcompareform.SpeedButton1Click(Sender: TObject);
  108. var orgf,crkf    : TMemoryStream;
  109.     porgb,pcrkb  : ^byte;
  110.     orgb,crkb    : byte;
  111.     position     : longint;
  112.     base         : dword;
  113.     changes      : longint;
  114. begin
  115. if edit1.text = edit2.text then
  116.   begin
  117.   mb('Please choose 2 different files !','!!!',mb_iconerror);
  118.   exit;
  119.   end;
  120. // load the files into the memory and check a bit around them
  121. orgf:=tmemorystream.create;
  122. crkf:=tmemorystream.create;
  123. orgf.LoadFromFile(edit1.text);
  124. crkf.loadfromfile(edit2.text);
  125. if orgf.Size <> crkf.Size then
  126.   begin
  127.   mb('Sorry...different filesizes aren''t supported !',':(',mb_iconerror);
  128.   orgf.free;
  129.   crkf.free;
  130.   exit;
  131.   end;
  132. if (orgf.size = 0) then
  133.   begin
  134.   mb('Please don''t choose any files with a filesize of 0 !','!!!',mb_iconerror);
  135.   orgf.free;
  136.   crkf.free;
  137.   exit;
  138.   end;
  139. // get the image base and the section header values (offset2va)
  140. dosh := PIMAGE_DOS_HEADER(orgf.memory);
  141. peh:= PIMAGE_NT_HEADERS(Longword(dosh) + Longword(dosh.e_lfanew));
  142. if (dosh.e_magic <> image_dos_signature) or (peh.Signature <> image_NT_signature) then
  143.   begin
  144.   mb('The File Compare dos only support PE files !','!!!',mb_iconerror);
  145.   orgf.free;
  146.   crkf.free;
  147.   exit;
  148.   end;
  149. sectionh:=PIMAGE_SECTION_HEADER(peh);
  150. // let's have some pointer action
  151. porgb:=orgf.Memory;
  152. pcrkb:=crkf.memory;
  153. gauge1.MaxValue:=orgf.size;
  154. // the gauge makes the search routine very slow but it looks cooler !
  155. application.processmessages;
  156. // go through the whole file
  157. changes:=0;
  158. for position:=0 to orgf.size do
  159.   begin
  160.   orgb:=porgb^;
  161.   crkb:=pcrkb^;
  162.   if orgb <> crkb then
  163.     begin
  164.     newitem:=form1.listview1.items.add;
  165.     newitem.caption:='Patch';
  166.     newitem.subitems.add(inttohex(offset2va(position),8));
  167.     newitem.subitems.add(inttohex(orgb,2));
  168.     newitem.subitems.add(inttohex(crkb,2));
  169.     inc(changes);
  170.     if changes = maxpatchbytesnum then
  171.       begin
  172.       mb('Oh !!...The Number of Patches is limited to 500 !'#13+
  173.          'The compare progress was aborted.',':(',mb_iconerror);
  174.       orgf.free;
  175.       crkf.free;
  176.       application.processmessages;
  177.       close;
  178.       exit;
  179.       end;
  180.     end;
  181.   inc(porgb);
  182.   inc(pcrkb);
  183.   gauge1.Progress:=position;//...faster :P
  184.   end;
  185. // clean up
  186. orgf.free;
  187. crkf.free;
  188. application.processmessages;
  189. close;
  190. end;
  191.  
  192. end.
  193.