| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008 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 | using System.Runtime.Serialization; |
|---|
| 26 | |
|---|
| 27 | using Eraser.Manager; |
|---|
| 28 | |
|---|
| 29 | namespace Eraser.DefaultPlugins |
|---|
| 30 | { |
|---|
| 31 | [Serializable] |
|---|
| 32 | class EraseCustom : PassBasedErasureMethod |
|---|
| 33 | { |
|---|
| 34 | /// <summary> |
|---|
| 35 | /// Constructor. |
|---|
| 36 | /// </summary> |
|---|
| 37 | /// <param name="method">The erasure method definition for the custom method.</param> |
|---|
| 38 | public EraseCustom(CustomErasureMethod method) |
|---|
| 39 | { |
|---|
| 40 | this.method = method; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// <summary> |
|---|
| 44 | /// Registers all defined custom methods with the method manager. |
|---|
| 45 | /// </summary> |
|---|
| 46 | internal static void RegisterAll() |
|---|
| 47 | { |
|---|
| 48 | if (!DefaultPlugin.Settings.ContainsKey("EraseCustom")) |
|---|
| 49 | return; |
|---|
| 50 | |
|---|
| 51 | Dictionary<Guid, CustomErasureMethod> methods = (Dictionary<Guid, CustomErasureMethod>) |
|---|
| 52 | DefaultPlugin.Settings["EraseCustom"]; |
|---|
| 53 | foreach (Guid guid in methods.Keys) |
|---|
| 54 | { |
|---|
| 55 | CustomErasureMethod method = methods[guid]; |
|---|
| 56 | ErasureMethodManager.Register(new EraseCustom(method), new object[] { method }); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public override string Name |
|---|
| 61 | { |
|---|
| 62 | get { return method.Name; } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public override Guid GUID |
|---|
| 66 | { |
|---|
| 67 | get { return method.GUID; } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | protected override bool RandomizePasses |
|---|
| 71 | { |
|---|
| 72 | get { return method.RandomizePasses; } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | protected override Pass[] PassesSet |
|---|
| 76 | { |
|---|
| 77 | get { return method.Passes; } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | CustomErasureMethod method; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /// <summary> |
|---|
| 84 | /// Contains information necessary to create user-defined erasure methods. |
|---|
| 85 | /// </summary> |
|---|
| 86 | [Serializable] |
|---|
| 87 | public class CustomErasureMethod : ISerializable |
|---|
| 88 | { |
|---|
| 89 | public CustomErasureMethod() |
|---|
| 90 | { |
|---|
| 91 | Name = string.Empty; |
|---|
| 92 | GUID = Guid.Empty; |
|---|
| 93 | RandomizePasses = true; |
|---|
| 94 | Passes = null; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public CustomErasureMethod(SerializationInfo info, StreamingContext context) |
|---|
| 98 | { |
|---|
| 99 | Name = info.GetString("Name"); |
|---|
| 100 | GUID = (Guid)info.GetValue("GUID", GUID.GetType()); |
|---|
| 101 | RandomizePasses = info.GetBoolean("RandomizePasses"); |
|---|
| 102 | List<PassData> passes = (List<PassData>) |
|---|
| 103 | info.GetValue("Passes", typeof(List<PassData>)); |
|---|
| 104 | |
|---|
| 105 | Passes = new ErasureMethod.Pass[passes.Count]; |
|---|
| 106 | for (int i = 0; i != passes.Count; ++i) |
|---|
| 107 | Passes[i] = passes[i]; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public string Name; |
|---|
| 111 | public Guid GUID; |
|---|
| 112 | public bool RandomizePasses; |
|---|
| 113 | public ErasureMethod.Pass[] Passes; |
|---|
| 114 | |
|---|
| 115 | #region ISerializable Members |
|---|
| 116 | public void GetObjectData(SerializationInfo info, StreamingContext context) |
|---|
| 117 | { |
|---|
| 118 | info.AddValue("Name", Name); |
|---|
| 119 | info.AddValue("GUID", GUID); |
|---|
| 120 | info.AddValue("RandomizePasses", RandomizePasses); |
|---|
| 121 | |
|---|
| 122 | List<PassData> passes = new List<PassData>(Passes.Length); |
|---|
| 123 | foreach (ErasureMethod.Pass pass in Passes) |
|---|
| 124 | passes.Add(new PassData(pass)); |
|---|
| 125 | info.AddValue("Passes", passes); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | [Serializable] |
|---|
| 129 | private class PassData |
|---|
| 130 | { |
|---|
| 131 | public PassData(ErasureMethod.Pass pass) |
|---|
| 132 | { |
|---|
| 133 | if (pass.Function == ErasureMethod.Pass.WriteConstant) |
|---|
| 134 | { |
|---|
| 135 | Random = false; |
|---|
| 136 | OpaqueValue = pass.OpaqueValue; |
|---|
| 137 | } |
|---|
| 138 | else if (pass.Function == ErasureMethod.Pass.WriteRandom) |
|---|
| 139 | { |
|---|
| 140 | Random = true; |
|---|
| 141 | OpaqueValue = null; |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | throw new NotImplementedException("The custom erasure method can only comprise " + |
|---|
| 145 | "passes containining constant or random passes"); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public static implicit operator ErasureMethod.Pass(PassData pass) |
|---|
| 149 | { |
|---|
| 150 | ErasureMethod.Pass result = new ErasureMethod.Pass(pass.Random ? |
|---|
| 151 | ErasureMethod.Pass.WriteRandom : |
|---|
| 152 | ErasureMethod.Pass.WriteConstant, pass.OpaqueValue); |
|---|
| 153 | return result; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | object OpaqueValue; |
|---|
| 157 | bool Random; |
|---|
| 158 | } |
|---|
| 159 | #endregion |
|---|
| 160 | } |
|---|
| 161 | } |
|---|