⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogproexe.pas

📁 详细的ERP设计资料
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  {$IFDEF TRIALRUN}
    _CC_; _VC_;
  {$ENDIF}
  Result := False;

  Fh := CreateFile(PAnsiChar(FileName), GENERIC_READ or GENERIC_WRITE, 0,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (Fh <> INVALID_HANDLE_VALUE) then begin
    Size := Windows.GetFileSize(Fh, nil);

    FileMap := CreateFileMapping(Fh, nil, PAGE_READWRITE, 0, 0, nil);
    if (FileMap <> 0) then begin
      Memory := MapViewOfFile(FileMap, FILE_MAP_WRITE, 0, 0, 0);
      if (Memory <> nil) then begin
        for I := 0 to (Size - SizeOf(TSignatureRec)) - 1 do begin
          if (PSignatureRec(@Memory[I])^.Sig1 = $407E7E21) and
             (PSignatureRec(@Memory[I])^.Sig2 = $33435243) and
             (PSignatureRec(@Memory[I])^.Sig3 = $7E7E4032) and
             (PSignatureRec(@Memory[I])^.Sig4 = $407E7E21) and
             (PSignatureRec(@Memory[I])^.Sig5 = $33435243) and
             (PSignatureRec(@Memory[I])^.Sig6 = $7E7E4032) then begin

            {found it}
            Sig := @Memory^[I];

            if EraseMarker then begin
              Sig.Sig1 := Sig.Sig1 xor timeGetTime;
              Sig.Sig2 := Sig.Sig2 xor timeGetTime;
              Sig.Sig3 := Sig.Sig3 xor timeGetTime;
              Sig.Sig4 := Sig.Sig4 xor timeGetTime;
              Sig.Sig5 := Sig.Sig5 xor timeGetTime;
              Sig.Sig6 := Sig.Sig6 xor timeGetTime;
            end;

            Sig.Offset := I;
            Sig.Size := Size;
            Sig.CRC := $FFF00FFF;  {special CRC init}

            {compute crc ignoring the signature record}
            UpdateCRC32(Sig.CRC, Memory^, Sig.Offset);
            UpdateCRC32(Sig.CRC, Memory^[Sig.Offset + SizeOf(TSignatureRec)],
              Sig.Size - (Sig.Offset + SizeOf(TSignatureRec)));
            Sig.CRC := not Sig.CRC;

            Result := True;
            Break;
          end;
        end;
        UnmapViewOfFile(Memory);
      end;
      CloseHandle(FileMap);
    end;
    CloseHandle(Fh);
  end;
end;
{$ELSE}
function ProtectExe(const FileName : string;  EraseMarker : Boolean) : Boolean;
  {-stamp exe with crc and file size. optionally erase search markers}
const
  BufSize = 4096; {must be larger than the size of the signature record}
var
  Fh        : THandle;
  I, Size   : LongInt;
  Sig       : TSignatureRec;
  Buf       : PAnsiChar;
  CRC       : LongInt;
  TotalRead : LongInt;
  BytesRead : LongInt;
begin
  {$IFDEF TRIALRUN}
    _CC_; _VC_;
  {$ENDIF}
  Result := False;

  Buf := StrAlloc(BufSize);
  try
    Fh := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
    if (Fh >= 0 {HFILE_ERROR}) then begin
      Size := FileSeek(Fh, 0, 2);
      FileSeek(Fh, 0, 0);  {reset to beginning of file}
      CRC := $FFF00FFF;    {special CRC init}
      TotalRead := 0;
      repeat
        BytesRead := FileRead(Fh, Buf^, BufSize);

        {search if not found}
        if not Result then begin
          for I := 0 to BytesRead - SizeOf(TSignatureRec) - 1 do begin
            if (PSignatureRec(@Buf[I])^.Sig1 = $407E7E21) and
               (PSignatureRec(@Buf[I])^.Sig2 = $33435243) and
               (PSignatureRec(@Buf[I])^.Sig3 = $7E7E4032) and
               (PSignatureRec(@Buf[I])^.Sig4 = $407E7E21) and
               (PSignatureRec(@Buf[I])^.Sig5 = $33435243) and
               (PSignatureRec(@Buf[I])^.Sig6 = $7E7E4032) then begin

              Result := True;  {found it}

              Sig := PSignatureRec(@Buf[I])^;
              if EraseMarker then begin
                Sig.Sig1 := Sig.Sig1 xor timeGetTime;
                Sig.Sig2 := Sig.Sig2 xor timeGetTime;
                Sig.Sig3 := Sig.Sig3 xor timeGetTime;
                Sig.Sig4 := Sig.Sig4 xor timeGetTime;
                Sig.Sig5 := Sig.Sig5 xor timeGetTime;
                Sig.Sig6 := Sig.Sig6 xor timeGetTime;
              end;

              Sig.Offset := TotalRead + I;
              Sig.Size := Size;

              {update crc up to the start of the signature record}
              UpdateCRC32(CRC, Buf^, I);
              {update crc for the remaing potion of the buffer (after the signature record)}
              UpdateCRC32(CRC, Buf[I+SizeOf(TSignatureRec)],
                          BytesRead-(I+SizeOf(TSignatureRec)));
            end;
          end;
        end;

        if not Result then begin
          UpdateCRC32(CRC, Buf^, BytesRead-SizeOf(TSignatureRec));
          {back up by size of TSignatureRec to insure that split record is found}
          FileSeek(Fh, -SizeOf(TSignatureRec), 1);
          Inc(TotalRead, BytesRead-SizeOf(TSignatureRec));
        end;
      until (BytesRead < BufSize) or Result;

      {scan remaining portion of file}
      if Result then begin
        repeat
          BytesRead := FileRead(Fh, Buf^, BufSize);
          UpdateCRC32(CRC, Buf^, BytesRead);
        until (BytesRead < BufSize);
      end;

      {save signature record back to the exe file}
      if Result then begin
        Sig.CRC := not CRC;
        FileSeek(Fh, Sig.Offset, 0);
        FileWrite(Fh, Sig, SizeOf(TSignatureRec))
      end;

      FileClose(Fh);
    end;
  finally
    StrDispose(Buf);
  end;
end;
{$ENDIF}


{$IFDEF Win32}
function UnprotectExe(const FileName : string) : Boolean;
  {-writes uninitialized signature record. marker must not have been erased}
var
  Fh      : THandle;
  FileMap : THandle;
  Memory  : PByteArray;
  I, Size : LongInt;
  Sig     : PSignatureRec;
begin
  {$IFDEF TRIALRUN}
    _CC_; _VC_;
  {$ENDIF}
  Result := False;

  Fh := CreateFile(PAnsiChar(FileName), GENERIC_READ or GENERIC_WRITE, 0,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (Fh <> INVALID_HANDLE_VALUE) then begin
    Size := Windows.GetFileSize(Fh, nil);

    FileMap := CreateFileMapping(Fh, nil, PAGE_READWRITE, 0, 0, nil);
    if (FileMap <> 0) then begin
      Memory := MapViewOfFile(FileMap, FILE_MAP_WRITE, 0, 0, 0);
      if (Memory <> nil) then begin
        for I := 0 to (Size - SizeOf(TSignatureRec)) - 1 do begin
          if (PSignatureRec(@Memory[I])^.Sig1 = $407E7E21) and
             (PSignatureRec(@Memory[I])^.Sig2 = $33435243) and
             (PSignatureRec(@Memory[I])^.Sig3 = $7E7E4032) and
             (PSignatureRec(@Memory[I])^.Sig4 = $407E7E21) and
             (PSignatureRec(@Memory[I])^.Sig5 = $33435243) and
             (PSignatureRec(@Memory[I])^.Sig6 = $7E7E4032) then begin

            {found it}
            Sig := @Memory^[I];

            {restore to uninitialized state}
            Sig.Offset := 1;                                         {!!.08}
            Sig.Size := 2;                                           {!!.08}
            Sig.CRC := 3;                                            {!!.08}

            Result := True;
            Break;
          end;
        end;
        UnmapViewOfFile(Memory);
      end;
      CloseHandle(FileMap);
    end;
    CloseHandle(Fh);
  end;
end;
{$ELSE}
function UnprotectExe(const FileName : string) : Boolean;
  {-writes uninitialized signature record. marker must not have been erased}
const
  BufSize = 4096; {must be larger than the size of the signature record}
var
  Fh        : THandle;
  I         : LongInt;
  Sig       : TSignatureRec;
  Buf       : PAnsiChar;
  Offset    : LongInt;
  BytesRead : LongInt;
  TotalRead : LongInt;
begin
  {$IFDEF TRIALRUN}
    _CC_; _VC_;
  {$ENDIF}
  Result := False;

  Buf := StrAlloc(BufSize);
  try
    Fh := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
    if (Fh >= 0 {HFILE_ERROR}) then begin
      FileSeek(Fh, 0, 0);  {reset to beginning of file}
      TotalRead := 0;
      repeat
        BytesRead := FileRead(Fh, Buf^, BufSize);

        {search if not found}
        if not Result then begin
          for I := 0 to BytesRead - SizeOf(TSignatureRec) - 1 do begin
            if (PSignatureRec(@Buf[I])^.Sig1 = $407E7E21) and
               (PSignatureRec(@Buf[I])^.Sig2 = $33435243) and
               (PSignatureRec(@Buf[I])^.Sig3 = $7E7E4032) and
               (PSignatureRec(@Buf[I])^.Sig4 = $407E7E21) and
               (PSignatureRec(@Buf[I])^.Sig5 = $33435243) and
               (PSignatureRec(@Buf[I])^.Sig6 = $7E7E4032) then begin

              {found it}
              Sig := PSignatureRec(@Buf[I])^;

              {save position in file}
              Offset := TotalRead + I;
              Result := True;
            end;
          end;
        end;

        if not Result then begin
          {back up by size of TSignatureRec to insure that split record is found}
          FileSeek(Fh, -SizeOf(TSignatureRec), 1);
          Inc(TotalRead, BytesRead-SizeOf(TSignatureRec));
        end;
      until (BytesRead < BufSize) or Result;

      {save signature record back to the exe file}
      if Result then begin
        {restore to uninitialized state}
        Sig.Offset := 1;                                             {!!.08}
        Sig.Size := 2;                                               {!!.08}
        Sig.CRC := 3;                                                {!!.08}

        FileSeek(Fh, Offset, 0);
        FileWrite(Fh, Sig, SizeOf(TSignatureRec))
      end;

      FileClose(Fh);
    end;
  finally
    StrDispose(Buf);
  end;
end;
{$ENDIF}


{checksum/CRC routines}

procedure UpdateChecksum(var Sum : LongInt;  const Buf;  BufSize : LongInt);
var
  Bytes : TByteArray absolute Buf;
  I     : LongInt;
begin
  for I := 0 to BufSize - 1 do
    Sum := Sum + Bytes[I];
end;

{$IFDEF Win32}
function FileCRC32(const FileName : string) : DWord;                 {!!.07}
var
  Fh      : THandle;
  FileMap : THandle;
  Size    : LongInt;
  Memory  : PByteArray;
  Buf     : array [0..MAX_PATH - 1] of AnsiChar;
begin
  Result := $FFFFFFFF;                                               {!!.07}
  StrPLCopy(Buf, FileName, SizeOf(Buf)-1);
  Fh := CreateFile(Buf, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
    nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if (Fh <> INVALID_HANDLE_VALUE) then begin                         {!!.07}
    Size := Windows.GetFileSize(Fh, nil);
    FileMap := CreateFileMapping(Fh, nil, PAGE_READONLY, 0, 0, nil);
    if (FileMap <> 0) then begin
      Memory := MapViewOfFile(FileMap, FILE_MAP_READ, 0, 0, 0);
      if (Memory <> nil) then begin
        Result := $FFF00FFF;  {special CRC init}
        UpdateCRC32(Result, Memory^, Size);
        UnmapViewOfFile(Memory);
      end;
      CloseHandle(FileMap);
    end;
    CloseHandle(Fh);
  end;
end;
{$ELSE}
function FileCRC32(const FileName : string) : LongInt;
const
  BufSize = 4096;
var
  Fh        : Integer;
  BytesRead : LongInt;
  Buf       : PAnsiChar;
begin
  Buf := StrAlloc(BufSize);
  try
    StrPLCopy(Buf, FileName, BufSize-1);
    Fh := FileOpen(StrPas(Buf), fmOpenRead or fmShareDenyWrite);
    if (Fh >= 0 {HFILE_ERROR}) then begin
      Result := $FFF00FFF;  {special CRC init}
      repeat
        BytesRead := FileRead(Fh, Buf^, BufSize);
        UpdateCRC32(Result, Buf^, BytesRead);
      until (BytesRead < BufSize);
      FileClose(Fh);
    end;
  finally
    StrDispose(Buf);
  end;
end;
{$ENDIF}

procedure UpdateCRC32(var CRC : DWord;  const Buf;  BufSize : LongInt); {!!.07}
var
  Bytes : TByteArray absolute Buf;
  I     : LongInt;
  B     : Byte;
begin
  for I := 0 to BufSize - 1 do begin
    B := TLongIntRec(CRC).LoLo xor Bytes[I];
    CRC := (CRC shr 8) xor CRC32Table[B];
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -