1

Topic: Search and Changeover of sequence byte in a binary file

I ask experts not to kick strongly the beginner, and to help to debug the following code:
We have a file " file.bin " in the length of the following test content 64 bytes (the first line-address):
00,01,02,03,04,05,06,07,08,09,0A,0B,0C,0D,0E,0F
FF,FF,FF,00,31,32,33,34,00,FF,FF,00,31,32,33,00
FF,FF,FF,FF,FF,FF,FF,00,31,32,33,34,00,FF,FF,FF
The program searches for certain sequences byte (an array " Poisk ") and replaces them with others (an array " Zamena ")

unit Main_Module;
interface
uses
Winapi. Windows, System. SysUtils, System. Classes;
Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. StdCtrls;
type
TMainForm = class (TForm)
OpenDialog: TOpenDialog;
btnStart: TButton;
btnOpenFile: TButton;
edtOpenFile: TEdit;
Memo: TMemo;
procedure btnOpenFileClick (Sender: TObject);
procedure btnStartClick (Sender: TObject);
private
{Private declarations}
FullFileName: String;//the Full file name
FBin: TFileStream;
public
{Public declarations}
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
//==================== Procedure of discovery BIN-FILE ============================
procedure TMainForm.btnOpenFileClick (Sender: TObject);
begin
if OpenDialog. Execute then FullFileName: = OpenDialog. FileName;
try
FBin: = TFileStream. Create (FullFileName, fmOpenRead);
edtOpenFile. Text: = FullFileName;
finally
FBin. Free;
end;
end;
//================= the End of procedure of discovery BIN-FILE =========================
//======================== Search and Changeover Procedure ===========================
procedure TMainForm.btnStartClick (Sender: TObject);
var
MBin: TMemoryStream;//a file in Memory
SizeFile: Dword;//Length of a file
pCurrent: Pointer;
P: PChar;
const
Poisk: array [0. 5] of Byte = ($00, $31, $32, $33, $34, $00);//the Search array
Zamena: array [0. 5] of Byte = ($00, $35, $36, $37, $38, $00);//the Changeover array
begin
try
FullFileName: = edtOpenFile. Text;//we read out file name
MBin: = TMemoryStream. Create;
MBin. LoadFromFile (FullFileName);
SizeFile: = MBin. Size;
==>//-------------------- the search and changeover Code------------------------------
==> pCurrent: = MBin. Memory;//the address in storage
==> P: = PChar (pCurrent);
==>//while Integer (pCurrent) + SizeOf (Poisk) <SizeBinFile do
==> while P <= (PChar (pCurrent) + SizeFile - length (Poisk)) do
==> begin
==> if CompareMem (P, @Poisk [0], length (Poisk)) then
==> begin//--- coincidence--- Is found
==> CopyMemory (P, @Zamena [0], length (Poisk));//we Copy Changeover instead of Search
==> P: = P + length (Poisk);//Correction for length of Search to search further
==> end
==> else
==> begin//--- There is no coincidence---------
==> inc (P);//is extendable to search further on a file
==> end;
==> end;
==>//-----------------------------------------------------------------------
finally
MBin. SaveToFile (FullFileName);//we Write  to a file
FreeAndNil (MBin);
end;
end;
//===================== the End of procedure of Search and Changeover ========================
end.

The code has been borrowed here from here: https://groups.google.com/forum/#!topic … 4HgdMDwpo;
But instead of FileMapping - the hectare has been made attempt to apply TMemoryStream
Something probably needs to be corrected a little in the selected piece of the code ;
The mind probably yet does not have also knowledge!... smile))

2

Re: Search and Changeover of sequence byte in a binary file

Bellic wrote:

P: PChar;

to Replace on PByte or PAnsiChar. It is unambiguous. If does not help, then spread here that was in a file and that became

3

Re: Search and Changeover of sequence byte in a binary file

[quote = _ Vasilisk _] it is passed...
To replace on PByte or PAnsiChar. It is unambiguous. If does not help, then spread here that was in a file and that became

Initially (With that code that is resulted above) the file did not change at all...
Now I will try to replace P: PChar on P: PByte or P: PAnsiChar ...