source: branches/eraser6/DefaultPlugins/EraseFirstLast16KB.cs @ 254

Revision 254, 2.5 KB checked in by lowjoel, 6 years ago (diff)

Slight mistake in data size calculation.

Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Eraser.Manager;
6using System.IO;
7
8namespace Eraser.DefaultPlugins
9{
10    class FirstLast16KB : ErasureMethod
11    {
12        public override string Name
13        {
14            get { return "First/last 16KB Erasure"; }
15        }
16
17        public override int Passes
18        {
19            get { return 0; } //Variable number, depending on defaults.
20        }
21
22        public override Guid GUID
23        {
24            get { return new Guid("{0C2E07BF-0207-49a3-ADE8-46F9E1499C01}"); }
25        }
26
27        public override long CalculateEraseDataSize(List<string> paths, long targetSize)
28        {
29            //Simple. Number of files multiplied by 32kb.
30            return paths.Count * dataSize * 2;
31        }
32
33        public override void Erase(Stream strm, long erasureLength, PRNG prng,
34            OnProgress callback)
35        {
36            //Make sure that the erasureLength passed in here is the maximum value
37            //for the size of long, since we don't want to write extra or write
38            //less.
39            if (erasureLength != long.MaxValue)
40                throw new ArgumentException("The amount of data erased should not be " +
41                    "limited, since this is a self-limiting erasure method.");
42
43            //Try to retrieve the default erasure method.
44            ErasureMethod defaultMethod = ErasureMethodManager.GetInstance(
45                Globals.Settings.DefaultFileErasureMethod);
46
47            //If we are the default, use the default pseudorandom pass.
48            if (defaultMethod.GUID == GUID)
49                defaultMethod = ErasureMethodManager.GetInstance(new Guid(
50                    "{BF8BA267-231A-4085-9BF9-204DE65A6641}"));
51
52            //If the target stream is shorter than 16kb, just forward it to the default
53            //function.
54            if (strm.Length < dataSize)
55            {
56                defaultMethod.Erase(strm, erasureLength, prng, callback);
57                return;
58            }
59
60            //Declare our local anonymous function to forward the progress event
61            //to the callback function.
62            float baseCompleted = 0;
63            OnProgress chainCallbackHandler = delegate(float currentProgress, int currentPass)
64            {
65                callback(baseCompleted + currentProgress / 2,
66                    (int)((baseCompleted * defaultMethod.Passes) + currentPass / 2));
67            };
68
69            //Seek to the beginning and write 16kb.
70            strm.Seek(0, SeekOrigin.Begin);
71            defaultMethod.Erase(strm, dataSize, prng, chainCallbackHandler);
72            baseCompleted = 0.5F;
73
74            //Seek to the end - 16kb, and write.
75            strm.Seek(-dataSize, SeekOrigin.End);
76            defaultMethod.Erase(strm, long.MaxValue, prng, chainCallbackHandler);
77        }
78
79        /// <summary>
80        /// The amount of data to be erased from the header and the end of the file.
81        /// </summary>
82        private const long dataSize = 16 * 1024;
83    }
84}
Note: See TracBrowser for help on using the repository browser.