| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | |
|---|
| 5 | using Eraser.Manager; |
|---|
| 6 | using System.IO; |
|---|
| 7 | |
|---|
| 8 | namespace 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 | //Amount of data required to be written. |
|---|
| 30 | long amountToWrite = 0; |
|---|
| 31 | if (paths == null) |
|---|
| 32 | { |
|---|
| 33 | if (targetSize <= dataSize) |
|---|
| 34 | amountToWrite = targetSize; |
|---|
| 35 | else |
|---|
| 36 | amountToWrite = dataSize * 2; |
|---|
| 37 | } |
|---|
| 38 | else |
|---|
| 39 | amountToWrite = paths.Count * dataSize * 2; |
|---|
| 40 | |
|---|
| 41 | //The final amount has to be multiplied by the number of passes. |
|---|
| 42 | return amountToWrite * Method.Passes; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public override void Erase(Stream strm, long erasureLength, PRNG prng, |
|---|
| 46 | OnProgress callback) |
|---|
| 47 | { |
|---|
| 48 | //Make sure that the erasureLength passed in here is the maximum value |
|---|
| 49 | //for the size of long, since we don't want to write extra or write |
|---|
| 50 | //less. |
|---|
| 51 | if (erasureLength != long.MaxValue) |
|---|
| 52 | throw new ArgumentException("The amount of data erased should not be " + |
|---|
| 53 | "limited, since this is a self-limiting erasure method."); |
|---|
| 54 | |
|---|
| 55 | ErasureMethod method = Method; |
|---|
| 56 | |
|---|
| 57 | //If the target stream is shorter than 16kb, just forward it to the default |
|---|
| 58 | //function. |
|---|
| 59 | if (strm.Length < dataSize) |
|---|
| 60 | { |
|---|
| 61 | method.Erase(strm, erasureLength, prng, callback); |
|---|
| 62 | return; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | //Seek to the beginning and write 16kb. |
|---|
| 66 | strm.Seek(0, SeekOrigin.Begin); |
|---|
| 67 | method.Erase(strm, dataSize, prng, callback); |
|---|
| 68 | |
|---|
| 69 | //Seek to the end - 16kb, and write. |
|---|
| 70 | strm.Seek(-dataSize, SeekOrigin.End); |
|---|
| 71 | method.Erase(strm, long.MaxValue, prng, callback); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | private ErasureMethod Method |
|---|
| 75 | { |
|---|
| 76 | get |
|---|
| 77 | { |
|---|
| 78 | //Try to retrieve the default erasure method. |
|---|
| 79 | ErasureMethod defaultMethod = ErasureMethodManager.GetInstance( |
|---|
| 80 | Globals.Settings.DefaultFileErasureMethod); |
|---|
| 81 | |
|---|
| 82 | //If we are the default, use the default pseudorandom pass. |
|---|
| 83 | if (defaultMethod.GUID == GUID) |
|---|
| 84 | defaultMethod = ErasureMethodManager.GetInstance(new Guid( |
|---|
| 85 | "{BF8BA267-231A-4085-9BF9-204DE65A6641}")); |
|---|
| 86 | |
|---|
| 87 | return defaultMethod; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /// <summary> |
|---|
| 92 | /// The amount of data to be erased from the header and the end of the file. |
|---|
| 93 | /// </summary> |
|---|
| 94 | private const long dataSize = 16 * 1024; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|