-
Content Count
24 -
Joined
-
Last visited
Community Reputation
0 NeutralTechnical Information
-
Delphi-Version
Delphi 10.2 Tokyo
-
Thanks, any plan to provide any working example with multiple search and replacement with faster method
-
Already provided working procedure by @PeterBelow , it works like charm as per my expectations only the problem is it gives error if file size is more than 2GB . Out of memory while expanding memory stream procedure ReplaceBytesInFile(aStream: TMemoryStream; const aSearchBytes, aReplaceBytes: TBytes); var LNumBytes: Integer; LPos, LEnd: PByte; begin LNumBytes := Length(aSearchBytes); Assert(LNumBytes = Length(aReplaceBytes), 'Arrays have to be of the same length!'); if LNumBytes = 0 then Exit; LPos := aStream.Memory; LEnd := LPos; Inc(LEnd, aStream.Size - LNumBytes ); while LPos < LEnd do begin if LPos^ = aSearchBytes[0] then begin if CompareMem(LPos, @aSearchBytes[0], LNumBytes) then begin CopyMemory(LPos, @aReplaceBytes[0], LNumBytes); // or // Move( aReplaceBytes[0], LPos^, LNumBytes ); Inc(LPos, LNumBytes); end else Inc(LPos); end else Inc(LPos); end; {while} end; type TDataRec = record SearchFor, ReplaceBy: TBytes end; const Data: array [0..1] of TDataRec = ( ( SearchFor: [$74, $00, $6f, $00, $7, $00, $73]; ReplaceBy: [$68, $00, $6f, $00, $7, $00, $73]), ( SearchFor: [$76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]; ReplaceBy: [$00, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]) // add more data as needed, adjust array upper bound accordingly ); procedure TForm2.Test; var I: Integer; theFile: string; LBuffer: TMemoryStream; begin theFile := 'pathname here'; LBuffer := TMemoryStream.Create(); try LBuffer.LoadFromFile(theFile); for I := Low(Data) to High(Data) do ReplaceBytesInFile(LBuffer, Data[I].SearchFor, Data[I].ReplaceBy); LBuffer.SaveToFile(theFile); finally LBuffer.Free; end; end;
-
I am still waiting for your help to solve the big file issue please
-
No not directly replacement of the original file but to create the copy of it.
-
I am really making you panic even I am trying on Intel(R) Core(TM) i7-7700HQ 16GB RAM and 2TB of SSD and using Original Windows 10 64bit . I am really surprised if it works on more 5GB of File why on my PC gives Out of memory while expanding memory stream. on 2.5GB of file. Is there any way to make Change of TMemoryStream to TFileStream in your procedure ? because this procedure is working as my expections only the problem is bigger file size
-
The Procedure works well on 2GB file but If the File size is above 2GB it gives error, Out of memory while expanding memory stream. I am using MSWindows 10 64bit and Delphi 10.2
-
If aStream is TFileStream then how to use LPos := aStream.Memory;
-
It is 32 bit Platform and the file size is 3GB
-
Thanks a lot for your help but I get error Out of memory while expanding memory stream.
-
Waiting for @PeterBelow response, he definitely may be having any easy logic like his previous function.
-
I have a binary file n which I need to make changes.
-
Thanks .. No Actually I am confused with var theFile: string; SearchFor, ReplaceWith: TBytes; begin SearchFor := [$76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00]; ReplaceWith := [$00, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $5F]; ReplaceBytesInFile(theFile, SearchFor, ReplaceWith); $76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00 $5F, $00, $74, $00, $7A, $00, $5F, $00, $00 $77, $00, $68, $00, $79, $00, $70, $00, $5F, $00, $00 $44, $10, $6D, $00, $6F, $00, $64, $00, $65, $00, $6D, $00, $5F, $00, $00 $69, $10, $00, $00, $6C, $00, $75, $00, $65, $00, $74, $00, $6F, $00, $6F, $00, $74, $00, $68, $00, $5F, $00, $00 $6F, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $73, $00, $65, $00, $63, $00, $61, $00, $70, $00, $70, $00, $5F, $00, $00 $65, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $5F, $00, $00 $00, $10, $61, $00, $00, $00, $6C, $00, $5F, $00, $00 $34, $10, $64, $00, $73, $00, $70, $00, $5F, $00, $00 $8C, $10, $6B, $00, $65, $00, $79, $00, $6D, $00, $61, $00, $73, $00, $74, $00, $65, $00, $72, $00, $5F, $00, $00 $3B, $00, $00, $00, $6F, $00, $6F, $00, $74, $00, $5F, $00, $00 How can declare other hex lines for variable SearchFor, ReplaceWith: TBytes; Thanks in advance
-
Will ReplaceBytesInFile function work for multiple hex changes like If I try to replace all these hex lines by other hex $76, $00, $61, $00, $6F, $00, $70, $00, $5F, $00, $00 $5F, $00, $74, $00, $7A, $00, $5F, $00, $00 $77, $00, $68, $00, $79, $00, $70, $00, $5F, $00, $00 $44, $10, $6D, $00, $6F, $00, $64, $00, $65, $00, $6D, $00, $5F, $00, $00 $69, $10, $00, $00, $6C, $00, $75, $00, $65, $00, $74, $00, $6F, $00, $6F, $00, $74, $00, $68, $00, $5F, $00, $00 $6F, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $73, $00, $65, $00, $63, $00, $61, $00, $70, $00, $70, $00, $5F, $00, $00 $65, $10, $6D, $00, $64, $00, $74, $00, $70, $00, $5F, $00, $00 $00, $10, $61, $00, $00, $00, $6C, $00, $5F, $00, $00 $34, $10, $64, $00, $73, $00, $70, $00, $5F, $00, $00 $8C, $10, $6B, $00, $65, $00, $79, $00, $6D, $00, $61, $00, $73, $00, $74, $00, $65, $00, $72, $00, $5F, $00, $00 $3B, $00, $00, $00, $6F, $00, $6F, $00, $74, $00, $5F, $00, $00
-
robertjohns changed their profile photo
-
Thanks the code works but I am sorry I did not explained my question properly . yes '.' represents 00 in hex viewer . HEX value for t.o.p.s is 74 00 6f 00 70 00 73 and HEX value for h.o.p.s is 68 00 6f 00 70 00 73 so I have to change hex value from 74 to 68 value should be changed by hex method not tops to hops Thanks in advance
-
StringReplace method will not work for this .. because we need to loop and find the all similar occurrences of t.o.p.s in whole the files and all the found occurrences need to be replaced by t to h and then after change need to save it.