| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | |
|---|
| 26 | using Eraser.Manager; |
|---|
| 27 | using Eraser.Util; |
|---|
| 28 | using System.IO; |
|---|
| 29 | using System.Threading; |
|---|
| 30 | using System.Windows.Forms; |
|---|
| 31 | |
|---|
| 32 | namespace Eraser.DefaultPlugins |
|---|
| 33 | { |
|---|
| 34 | sealed class FirstLast16KB : ErasureMethod |
|---|
| 35 | { |
|---|
| 36 | public FirstLast16KB() |
|---|
| 37 | { |
|---|
| 38 | try |
|---|
| 39 | { |
|---|
| 40 | //Try to retrieve the set erasure method |
|---|
| 41 | if (DefaultPlugin.Settings.FL16Method != Guid.Empty) |
|---|
| 42 | method = ErasureMethodManager.GetInstance( |
|---|
| 43 | DefaultPlugin.Settings.FL16Method); |
|---|
| 44 | else if (ManagerLibrary.Settings.DefaultFileErasureMethod != Guid) |
|---|
| 45 | method = ErasureMethodManager.GetInstance( |
|---|
| 46 | ManagerLibrary.Settings.DefaultFileErasureMethod); |
|---|
| 47 | else |
|---|
| 48 | method = ErasureMethodManager.GetInstance(new Gutmann().Guid); |
|---|
| 49 | } |
|---|
| 50 | catch (ErasureMethodNotFoundException) |
|---|
| 51 | { |
|---|
| 52 | MessageBox.Show(S._("The First/last 16KB erasure method " + |
|---|
| 53 | "requires another erasure method to erase the file.\n\nThis must " + |
|---|
| 54 | "be set in the Plugin Settings dialog."), Name, MessageBoxButtons.OK, |
|---|
| 55 | MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, |
|---|
| 56 | S.IsRightToLeft(null) ? MessageBoxOptions.RtlReading : 0); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public override string Name |
|---|
| 61 | { |
|---|
| 62 | get { return S._("First/last 16KB Erasure"); } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public override int Passes |
|---|
| 66 | { |
|---|
| 67 | get { return 0; } //Variable number, depending on defaults. |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | public override Guid Guid |
|---|
| 71 | { |
|---|
| 72 | get { return new Guid("{0C2E07BF-0207-49a3-ADE8-46F9E1499C01}"); } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public override long CalculateEraseDataSize(ICollection<string> paths, long targetSize) |
|---|
| 76 | { |
|---|
| 77 | //If we have no default or we are the default then throw an exception |
|---|
| 78 | if (method == null || method.Guid == Guid) |
|---|
| 79 | throw new InvalidOperationException(S._("The First/last 16KB erasure method " + |
|---|
| 80 | "requires another erasure method to erase the file.\n\nThis must " + |
|---|
| 81 | "be set in the Plugin Settings dialog.")); |
|---|
| 82 | |
|---|
| 83 | //Amount of data required to be written. |
|---|
| 84 | long amountToWrite = 0; |
|---|
| 85 | if (paths == null) |
|---|
| 86 | { |
|---|
| 87 | if (targetSize <= DataSize) |
|---|
| 88 | amountToWrite = targetSize; |
|---|
| 89 | else |
|---|
| 90 | amountToWrite = DataSize * 2; |
|---|
| 91 | } |
|---|
| 92 | else |
|---|
| 93 | amountToWrite = paths.Count * DataSize * 2; |
|---|
| 94 | |
|---|
| 95 | //The final amount has to be multiplied by the number of passes. |
|---|
| 96 | return amountToWrite * method.Passes; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public override void Erase(Stream strm, long erasureLength, Prng prng, |
|---|
| 100 | ErasureMethodProgressFunction callback) |
|---|
| 101 | { |
|---|
| 102 | //If we have no default or we are the default then throw an exception |
|---|
| 103 | if (method == null || method.Guid == Guid) |
|---|
| 104 | throw new InvalidOperationException(S._("The First/last 16KB erasure method " + |
|---|
| 105 | "requires another erasure method to erase the file.\n\nThis must " + |
|---|
| 106 | "be set in the Plugin Settings dialog.")); |
|---|
| 107 | |
|---|
| 108 | //Make sure that the erasureLength passed in here is the maximum value |
|---|
| 109 | //for the size of long, since we don't want to write extra or write |
|---|
| 110 | //less. |
|---|
| 111 | if (erasureLength != long.MaxValue) |
|---|
| 112 | throw new ArgumentException(S._("The amount of data erased should not be " + |
|---|
| 113 | "limited, since this is a self-limiting erasure method.")); |
|---|
| 114 | |
|---|
| 115 | //If the target stream is shorter than or equal to 32kb, just forward it to |
|---|
| 116 | //the default function. |
|---|
| 117 | if (strm.Length < DataSize * 2) |
|---|
| 118 | { |
|---|
| 119 | method.Erase(strm, erasureLength, prng, callback); |
|---|
| 120 | return; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | //We need to intercept the callback function as we run the erasure method |
|---|
| 124 | //twice on two parts of the file. |
|---|
| 125 | long dataSize = method.CalculateEraseDataSize(null, DataSize * 2); |
|---|
| 126 | ErasureMethodProgressFunction customCallback = |
|---|
| 127 | delegate(long lastWritten, long totalData, int currentPass) |
|---|
| 128 | { |
|---|
| 129 | callback(lastWritten, dataSize, currentPass); |
|---|
| 130 | }; |
|---|
| 131 | |
|---|
| 132 | //Seek to the beginning and write 16kb. |
|---|
| 133 | strm.Seek(0, SeekOrigin.Begin); |
|---|
| 134 | method.Erase(strm, dataSize, prng, callback == null ? null: customCallback); |
|---|
| 135 | |
|---|
| 136 | //Seek to the end - 16kb, and write. |
|---|
| 137 | strm.Seek(-dataSize, SeekOrigin.End); |
|---|
| 138 | method.Erase(strm, long.MaxValue, prng, callback == null ? null : customCallback); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /// <summary> |
|---|
| 142 | /// The amount of data to be erased from the header and the end of the file. |
|---|
| 143 | /// </summary> |
|---|
| 144 | private const long DataSize = 16 * 1024; |
|---|
| 145 | |
|---|
| 146 | private ErasureMethod method; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|