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

📄 freeotfefrmcdbbackuprestore.pas

📁 文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2, MD4, MD5, RIPEMD-128, RIPEMD-160, SHA-1, SHA-224, SHA-256,
💻 PAS
字号:
unit FreeOTFEfrmCDBBackupRestore;
// Description: 
// By Sarah Dean
// Email: sdean12@sdean12.org
// WWW:   http://www.FreeOTFE.org/
//
// -----------------------------------------------------------------------------
//


interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Spin64, StdCtrls,
  OTFEFreeOTFE_U, Buttons;

type
  TCDBOperationType = (opBackup, opRestore);

  TfrmFreeOTFECDBBackupRestore = class(TForm)
    pbCancel: TButton;
    pbOK: TButton;
    gbDest: TGroupBox;
    lblFileDescDest: TLabel;
    edFilenameDest: TEdit;
    lblOffsetDestBytes: TLabel;
    se64OffsetDest: TSpinEdit64;
    lblOffsetDest: TLabel;
    gbSrc: TGroupBox;
    lblFileDescSrc: TLabel;
    lblOffsetSrcBytes: TLabel;
    lblOffsetSrc: TLabel;
    edFilenameSrc: TEdit;
    se64OffsetSrc: TSpinEdit64;
    OpenDialog: TOpenDialog;
    SaveDialog: TSaveDialog;
    bbBrowsePartitionSrc: TBitBtn;
    bbBrowseFileSrc: TBitBtn;
    bbBrowseFileDest: TBitBtn;
    bbBrowsePartitionDest: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure pbOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ControlChange(Sender: TObject);
    procedure bbBrowseFileSrcClick(Sender: TObject);
    procedure bbBrowseFileDestClick(Sender: TObject);
    procedure bbBrowsePartitionSrcClick(Sender: TObject);
    procedure bbBrowsePartitionDestClick(Sender: TObject);
  protected
    FDlgType: TCDBOperationType;

    function  GetSrcFilename(): string;
    function  GetSrcOffset(): int64;
    function  GetDestFilename(): string;
    function  GetDestOffset(): int64;

    function  SanityCheckBackup(): boolean;
    function  SanityCheckRestore(): boolean;

    procedure EnableDisableControls();

  public
    OTFEFreeOTFE: TOTFEFreeOTFE;
    
    property DlgType: TCDBOperationType read FDlgType write FDlgType;

    property SrcFilename: string read GetSrcFilename;
    property SrcOffset: int64 read GetSrcOffset;
    property DestFilename: string read GetDestFilename;
    property DestOffset: int64 read GetDestOffset;

  end;


implementation

{$R *.DFM}

uses
  SDUGeneral,
  SDUDialogs,
  OTFEFreeOTFE_DriverAPI;  // Required for CRITICAL_DATA_LENGTH

const
  CRLF = #10+#13;


procedure TfrmFreeOTFECDBBackupRestore.FormShow(Sender: TObject);
begin
  edFilenameSrc.Text   := '';
  se64OffsetSrc.Value  := 0;
  edFilenameDest.Text  := '';
  se64OffsetDest.Value := 0;

  if (DlgType = opBackup) then
    begin
    self.caption := 'Backup Volume Critical Data Block';

    gbSrc.Caption          := 'Volume details';
    lblFileDescSrc.caption := '&Volume:';

    gbDest.Caption          := 'Backup details';
    lblFileDescDest.caption := '&Backup filename:';

    // Backup file starts from 0 - don't allow the user to change offset
    SDUEnableControl(se64OffsetDest, FALSE);
    SDUEnableControl(lblOffsetDest, FALSE);
    SDUEnableControl(lblOffsetDestBytes, FALSE);

    // Eliminate redundant partition button, remove image from remaining button
    // replace with "..." text
    bbBrowsePartitionDest.Visible := FALSE;
    edFilenameDest.Width := edFilenameDest.Width +
                            ((bbBrowseFileDest.Left + bbBrowseFileDest.Width) - (edFilenameDest.Left + edFilenameDest.Width));
    bbBrowseFileDest.Left := bbBrowsePartitionDest.Left;

    bbBrowseFileDest.Caption := '...';
    bbBrowseFileDest.Glyph := nil;
    end
  else
    begin
    self.caption := 'Restore Volume Critical Data Block';

    gbSrc.Caption          := 'Backup details';
    lblFileDescSrc.caption := '&Backup filename:';

    gbDest.Caption          := 'Volume details';
    lblFileDescDest.caption := '&Volume:';

    // Backup file starts from 0 - don't allow the user to change offset
    SDUEnableControl(se64OffsetSrc, FALSE);
    SDUEnableControl(lblOffsetSrc, FALSE);
    SDUEnableControl(lblOffsetSrcBytes, FALSE);

    // Eliminate redundant partition button, remove image from remaining button
    // replace with "..." text
    bbBrowsePartitionSrc.Visible := FALSE;
    edFilenameSrc.Width := edFilenameSrc.Width +
                            ((bbBrowseFileSrc.Left + bbBrowseFileSrc.Width) - (edFilenameSrc.Left + edFilenameSrc.Width));
    bbBrowseFileSrc.Left := bbBrowsePartitionSrc.Left;

    bbBrowseFileSrc.Caption := '...';
    bbBrowseFileSrc.Glyph := nil;
    end;


  EnableDisableControls();

end;


procedure TfrmFreeOTFECDBBackupRestore.FormCreate(Sender: TObject);
begin
  // Default to backup...
  DlgType := opBackup;

end;


// Sanity check a BACKUP operation
// Returns TRUE if all values entered make sense, otherwise FALSE
function TfrmFreeOTFECDBBackupRestore.SanityCheckBackup(): boolean;
var
  allOK: boolean;
begin
  allOK:= TRUE;

  // BACKUP SANITY CHECKS

  // If we're backing up, the destination filename MUST NOT EXIST.
  // This is done for safety - if we just prompted the user and asked them
  // to continue, the user may just slam "YES!" without checking - and if
  // they've transposed the filenames, this would overwrite the volume file!
  // Note: You can only backup to a *file*
  if FileExists(DestFilename) then
    begin
    SDUMessageDlg('The file you are attempting to backup to already exists.'+CRLF+
               CRLF+
               'Please delete this backup file and try again, or specify a '+CRLF+
               'different filename to backup to.',
               mtError, [mbOK], 0);
    allOK := FALSE;
    end;

  // Note: We're not worried if the source doens't exist - the backup operation
  //       will simply fail in this case

  Result := allOK;
end;


// Sanity check a RESTORE operation
// Returns TRUE if all values entered make sense, otherwise FALSE
function TfrmFreeOTFECDBBackupRestore.SanityCheckRestore(): boolean;
var
  allOK: boolean;
  confirm: DWORD;
  srcSize: LARGE_INTEGER;
begin
  allOK:= FALSE;

  // RESTORE SANITY CHECKS

  // Ensure that both the source (backup) and destination (volume) files
  // exist
  // Note: You can only restore from a *file*
  if not(FileExists(SrcFilename)) then
    begin
    SDUMessageDlg('The backup file specified does not exist.', mtError, [mbOK], 0);
    end
  else
    begin
    // Note: We're not worried if the destination doens't exist - the restore
    //       operation will simply fail in this case

    // Check that the source file is the right size for a backup file
    srcSize := SDUGetFileSize(SrcFilename);
    if (srcSize.QuadPart <> (CRITICAL_DATA_LENGTH div 8)) then
      begin
      SDUMessageDlg('The source file you specified does not appear to be the right '+CRLF+
                 'size for a backup file '+CRLF+
                 CRLF+
                 'Please check your settings and try again.',
                 mtError, [mbOK], 0);
      end
    else
      begin
      // Get the user to confirm the restoration

      // We don't need this confirmation when BACKING UP - only when RESTORING
      // Backup is relativly safe as it backs up to a file which doesn't exist
      confirm := SDUMessageDlg('Please confirm: Do you wish to restore the critial data '+CRLF+
                             'block from backup file:'+CRLF+
                             CRLF+
                             SrcFilename+CRLF+
                             CRLF+
                             'Into the volume:'+CRLF+
                             CRLF+
                             DestFilename+CRLF+
                             CRLF+
                             'Starting from offset '+inttostr(DestOffset)+' in the volume?',
                             mtConfirmation, [mbYes,mbNo], 0);

      if (confirm = mrYes) then
        begin
        allOK := TRUE;
        end;

      end;  // ELSE PART - if (not(CheckDestLarger())) then

    end;  // ELSE PART - if not(FileExists(GetSrcFilename()) then


  Result := allOK;
end;


procedure TfrmFreeOTFECDBBackupRestore.pbOKClick(Sender: TObject);
const
  USE_NOT_IN_USE = 'Please ensure that the volume is not mounted, or otherwise in use';
var
  allOK: boolean;
  operation: string;
begin
  allOK := FALSE;

  if (DlgType = opBackup) then
    begin
    operation := 'Backup';
    if SanityCheckBackup() then
      begin
      if OTFEFreeOTFE.BackupVolumeCriticalData(
                                       SrcFilename,
                                       SrcOffset,
                                       DestFilename
                                      ) then
        begin
        SDUMessageDlg('Backup operation completed successfully.', mtInformation, [mbOK], 0);
        allOK:= TRUE;
        end
      else
        begin
        SDUMessageDlg(
                   'Backup operation failed.'+CRLF+
                   CRLF+
                   USE_NOT_IN_USE,
                   mtError, [mbOK], 0
                  );
        end;

      end;

    end  // if (dlgType = opBackup) then
  else
    begin
    operation := 'Restore';
    if SanityCheckRestore() then
      begin
      if OTFEFreeOTFE.RestoreVolumeCriticalData(
                                   SrcFilename,
                                   DestFilename,
                                   DestOffset
                                  ) then
        begin
        SDUMessageDlg('Restore operation completed successfully.', mtInformation, [mbOK], 0);
        allOK:= TRUE;
        end
      else
        begin
        SDUMessageDlg(
                   'Restore operation failed.'+CRLF+
                   CRLF+
                   USE_NOT_IN_USE,
                   mtError, [mbOK], 0
                  );
        end;

      end;

    end;  // ELSE PART - if (dlgType = opBackup) then


  if (allOK) then
    begin
    ModalResult := mrOK;
    end;

end;

function TfrmFreeOTFECDBBackupRestore.GetSrcFilename(): string;
begin
  Result := edFilenameSrc.text;
end;

function TfrmFreeOTFECDBBackupRestore.GetSrcOffset(): int64;
begin
  Result := se64OffsetSrc.Value;
end;

function TfrmFreeOTFECDBBackupRestore.GetDestFilename(): string;
begin
  Result := edFilenameDest.text;
end;

function TfrmFreeOTFECDBBackupRestore.GetDestOffset(): int64;
begin
  Result := se64OffsetDest.Value;
end;



procedure TfrmFreeOTFECDBBackupRestore.ControlChange(
  Sender: TObject);
begin
  EnableDisableControls();

end;

procedure TfrmFreeOTFECDBBackupRestore.EnableDisableControls();
begin
  // Src and dest must be specified, and different
  // Offsets must be 0 or +ve
  pbOK.Enabled :=
                  (edFilenameSrc.text <> '')  AND
                  (edFilenameDest.text <> '') AND
                  (edFilenameSrc.text <> edFilenameDest.text) AND
                  (se64OffsetSrc.Value >= 0)  AND
                  (se64OffsetDest.Value >= 0);

end;


procedure TfrmFreeOTFECDBBackupRestore.bbBrowseFileSrcClick(
  Sender: TObject);
begin
  if (FDlgType = opBackup) then
    begin
    OpenDialog.Filter     := FILE_FILTER_FLT_VOLUMESANDKEYFILES;
    OpenDialog.DefaultExt := FILE_FILTER_DFLT_NONE;
    end
  else
    begin
    OpenDialog.Filter     := FILE_FILTER_FLT_CDBBACKUPS;
    OpenDialog.DefaultExt := FILE_FILTER_DFLT_CDBBACKUPS;
    end;

  SDUOpenSaveDialogSetup(OpenDialog, edFilenameSrc.text);

  if OpenDialog.Execute() then
    begin
    edFilenameSrc.text := OpenDialog.Filename;
    end;

end;

procedure TfrmFreeOTFECDBBackupRestore.bbBrowseFileDestClick(
  Sender: TObject);
begin
  if (FDlgType = opBackup) then
    begin
    SaveDialog.Filter     := FILE_FILTER_FLT_CDBBACKUPS;
    SaveDialog.DefaultExt := FILE_FILTER_DFLT_CDBBACKUPS;
    end
  else
    begin
    SaveDialog.Filter     := FILE_FILTER_FLT_VOLUMESANDKEYFILES;
    SaveDialog.DefaultExt := FILE_FILTER_DFLT_NONE;
    end;

  SDUOpenSaveDialogSetup(SaveDialog, edFilenameDest.text);

  if SaveDialog.Execute() then
    begin
    edFilenameDest.text := SaveDialog.Filename;
    end;

end;

procedure TfrmFreeOTFECDBBackupRestore.bbBrowsePartitionSrcClick(
  Sender: TObject);
var
  selectedPartition: string;
begin
  selectedPartition := OTFEFreeOTFE.SelectPartition();
  if (selectedPartition <> '') then
    begin
    edFilenameSrc.text := selectedPartition;
    end;

end;

procedure TfrmFreeOTFECDBBackupRestore.bbBrowsePartitionDestClick(
  Sender: TObject);
var
  selectedPartition: string;
begin
  selectedPartition := OTFEFreeOTFE.SelectPartition();
  if (selectedPartition <> '') then
    begin
    edFilenameDest.text := selectedPartition;
    end;

end;

END.


⌨️ 快捷键说明

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