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
|
unit FRemplacerIp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellApi, Math, CheckLst;
type
TIP = record
case integer of
0 : (ip: array[0..3] of byte);
1 : (byte1, byte2, byte3, byte4: byte)
end;
TForm2 = class(TForm)
btnRechercher: TButton;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
clbPositions: TCheckListBox;
btnRemplacer: TButton;
procedure btnRechercherClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnRemplacerClick(Sender: TObject);
private
{ Déclarations privées }
procedure RemplacerLesIps;
procedure SetEditIp(const AIp: TIP);
public
{ Déclarations publiques }
end;
var
Form2: TForm2;
const
//les constantes a rechercher
SeekIp : TIP = (ip:(192,168,1,1));
NewIp : TIP = (ip:(1,2,3,4));
implementation
{$R *.dfm}
procedure TForm2.btnRechercherClick(Sender: TObject);
const
BUFFSIZE = 100;
var
fs: TFileStream;
buff: array[0..BUFFSIZE-1] of byte;
i, cnt: integer;
begin
//Principe: on lit le fichier, et on recherche si on trouve l'ip.
// Quand on arrive a la fin du buffer lu, on recule la position dans le stream
// pour traiter les bytes qui ne forment pas un ip complete
clbPositions.Items.Clear;
if not OpenDialog1.Execute then
exit;
clbPositions.Items.BeginUpdate;
fs := TFileStream.Create(OpenDialog1.FileName, fmOpenRead or fmShareDenyWrite);
fs.Position := 0;
repeat
cnt := fs.Read(buff, BUFFSIZE);
//on compare jusqu'a ce qu'on ai plus assez de byte pour faire un TIP
for i := 0 to cnt-sizeof(TIP) do
begin
if CompareMem(@buff[i], @SeekIp, sizeOf(TIP)) then
begin //on a trouve l'ip
clbPositions.Items.Add(IntToStr(fs.Position-cnt+i));
clbPositions.Checked[clbPositions.Count-1] := true;
end;
end;
//si on est pas a la fin, on recule la position pour traiter les byte lus qui ne tiennent pas dans un TIP
if fs.Position < fs.Size-1 then
begin
fs.Position := fs.Position - sizeOf(TIP)+1;
end;
until cnt = 0;
fs.Free;
clbPositions.Items.EndUpdate;
MessageDlg('Recherche terminée', mtInformation, [mbOK], 0);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
OpenDialog1.FileName := Application.ExeName;
SetEditIp(SeekIp);
end;
procedure TForm2.RemplacerLesIps;
var
i: integer;
p: int64;
fs: TFileStream;
newName: string;
begin
//Remplace les adresse cochees dans la listbox par la NewIp
newName := ChangeFileExt(OpenDialog1.FileName, '.newip.exe');
if CopyFile(PChar(OpenDialog1.FileName), PChar(newName), false) then
begin
fs := TFileStream.Create(newName, fmOpenReadWrite or fmShareExclusive);
for i := 0 to clbPositions.Count-1 do
begin
if clbPositions.Checked[i] then
begin
p := StrToInt64(clbPositions.Items[i]);
fs.Position := p;
fs.Write(newIp, SizeOf(TIP));
end;
end;
fs.Free;
ShellExecute(0, 'open', PChar(newName), nil, nil, SW_SHOW);
end
else
showmessage('erreur creation exe');
end;
procedure TForm2.btnRemplacerClick(Sender: TObject);
begin
RemplacerLesIps;
end;
procedure TForm2.SetEditIp(const AIp: TIP);
begin
Edit1.Text := Inttostr(AIp.ip[0]) +'.'+ Inttostr(AIp.ip[1]) +'.'+ Inttostr(AIp.ip[2]) +'.'+ Inttostr(AIp.ip[3]);
end;
end. |
Partager