source: trunk/eraser/Eraser.DefaultPlugins/ErasureMethods/FirstLast16KB.cs @ 2515

Revision 2515, 4.9 KB checked in by lowjoel, 2 years ago (diff)

Set svn:keywords and svn:eol-style on all the source files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Runtime.InteropServices;
26
27using System.IO;
28using System.Threading;
29using System.Windows.Forms;
30
31using Eraser.Util;
32using Eraser.Plugins;
33using Eraser.Plugins.ExtensionPoints;
34
35namespace Eraser.DefaultPlugins
36{
37    [Guid("0C2E07BF-0207-49a3-ADE8-46F9E1499C01")]
38    sealed class FirstLast16KB : ErasureMethodBase
39    {
40        public FirstLast16KB()
41        {
42            try
43            {
44                //Try to retrieve the set erasure method
45                if (DefaultPlugin.Settings.FL16Method != Guid.Empty)
46                    method = Host.Instance.ErasureMethods[DefaultPlugin.Settings.FL16Method];
47                else if (Host.Instance.Settings.DefaultFileErasureMethod != Guid)
48                    method = Host.Instance.ErasureMethods[
49                        Host.Instance.Settings.DefaultFileErasureMethod];
50                else
51                    method = Host.Instance.ErasureMethods[new Gutmann().Guid];
52            }
53            catch (ErasureMethodNotFoundException)
54            {
55                MessageBox.Show(S._("The First/last 16KB erasure method " +
56                    "requires another erasure method to erase the file.\n\nThis must " +
57                    "be set in the Plugin Settings dialog."), Name, MessageBoxButtons.OK,
58                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
59                    Localisation.IsRightToLeft(null) ?
60                        MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign : 0);
61            }
62        }
63
64        public override string Name
65        {
66            get { return S._("First/last 16KB Erasure"); }
67        }
68
69        public override int Passes
70        {
71            get { return 0; } //Variable number, depending on defaults.
72        }
73
74        public override Guid Guid
75        {
76            get { return GetType().GUID; }
77        }
78
79        public override long CalculateEraseDataSize(ICollection<StreamInfo> paths, long targetSize)
80        {
81            //If we have no default or we are the default then throw an exception
82            if (method == null || method.Guid == Guid)
83                throw new InvalidOperationException(S._("The First/last 16KB erasure method " +
84                    "requires another erasure method to erase the file.\n\nThis must " +
85                    "be set in the Plugin Settings dialog."));
86
87            //Amount of data required to be written.
88            long amountToWrite = 0;
89            if (paths == null)
90            {
91                if (targetSize <= DataSize)
92                    amountToWrite = targetSize;
93                else
94                    amountToWrite = DataSize * 2;
95            }
96            else
97                amountToWrite = paths.Count * DataSize * 2;
98
99            //The final amount has to be multiplied by the number of passes.
100            return amountToWrite * method.Passes;
101        }
102
103        public override void Erase(Stream strm, long erasureLength, IPrng prng,
104            ErasureMethodProgressFunction callback)
105        {
106            //If we have no default or we are the default then throw an exception
107            if (method == null || method.Guid == Guid)
108                throw new InvalidOperationException(S._("The First/last 16KB erasure method " +
109                    "requires another erasure method to erase the file.\n\nThis must " +
110                    "be set in the Plugin Settings dialog."));
111
112            //Make sure that the erasureLength passed in here is the maximum value
113            //for the size of long, since we don't want to write extra or write
114            //less.
115            if (erasureLength != long.MaxValue)
116                throw new ArgumentException("The amount of data erased should not be " +
117                    "limited, since this is a self-limiting erasure method.");
118
119            //If the target stream is shorter than or equal to 32kb, just forward it to
120            //the default function.
121            if (strm.Length < DataSize * 2)
122            {
123                method.Erase(strm, erasureLength, prng, callback);
124                return;
125            }
126
127            //We need to intercept the callback function as we run the erasure method
128            //twice on two parts of the file.
129            long dataSize = method.CalculateEraseDataSize(null, DataSize * 2);
130            ErasureMethodProgressFunction customCallback =
131                delegate(long lastWritten, long totalData, int currentPass)
132                {
133                    callback(lastWritten, dataSize, currentPass);
134                };
135
136            //Seek to the beginning and write 16kb.
137            strm.Seek(0, SeekOrigin.Begin);
138            method.Erase(strm, dataSize, prng, callback == null ? null: customCallback);
139
140            //Seek to the end - 16kb, and write.
141            strm.Seek(-dataSize, SeekOrigin.End);
142            method.Erase(strm, long.MaxValue, prng, callback == null ? null : customCallback);
143        }
144
145        /// <summary>
146        /// The amount of data to be erased from the header and the end of the file.
147        /// </summary>
148        private const long DataSize = 16 * 1024;
149
150        private IErasureMethod method;
151    }
152}
Note: See TracBrowser for help on using the repository browser.