Показать сообщение отдельно
  #12  
Старый 21.07.2020, 12:18
Аватар для Vayrus
Vayrus Vayrus вне форума
Исполняемый Ретровирус
 
Регистрация: 09.08.2008
Адрес: Umbrella Corporation
Сообщения: 743
Репутация: 1293
По умолчанию

Автор, вот еще пример, дорабатывайте на разные размеры исходных и конечных данных + прикрутите отлов исключений:

Код:
const
  OriginalByte: array [0 .. 55] of Byte = ($64, $69, $6E, $67, $20, $6D, $65,
    $20, $68, $69, $73, $00, $00, $00, $00, $27, $00, $00, $28, $00, $00, $00,
    $21, $00, $00, $00, $63, $6F, $6D, $70, $75, $74, $65, $72, $2E, $00, $00,
    $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $27, $00, $00, $28,
    $00, $00, $00, $00);

  BytetoWrite: array [0 .. 55] of Byte = ($63, $68, $6F, $67, $20, $6D, $65,
    $20, $68, $69, $73, $00, $00, $00, $00, $27, $00, $00, $28, $00, $00, $00,
    $21, $00, $00, $00, $63, $6F, $6D, $70, $75, $74, $65, $72, $2E, $00, $00,
    $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $27, $00, $00, $28,
    $00, $00, $00, $06);

procedure DoMyPatch(const FileName: String);
var
  i: Int64;
  FileName: string;
  input: TFileStream;
  FileByteArray, ExtractedByteArray: Tbytes;
begin
  input := TFileStream.Create(FileName, fmOpenReadWrite);
  try
  input.Position := 0;
  SetLength(FileByteArray, input.size);
  input.Read(FileByteArray[0], Length(FileByteArray));
  for i := Low(FileByteArray) to High(FileByteArray) do
  begin
    ExtractedByteArray := Copy(FileByteArray, i, Length(OriginalByte));
    if CompareMem(@FileByteArray[i], @OriginalByte[0], Length(OriginalByte)) = True then
    begin
      input.Seek(i, SoFromBeginning);
      input.Write(BytetoWrite[0], Length(BytetoWrite));
    end;
  end;
  finally
    FreeAndNil(input);
  end;
end;
Ответить с цитированием