source: branches/eraser6/CodeReview/Eraser.DefaultPlugins/ErasureMethods/Custom.cs @ 1792

Revision 1792, 4.2 KB checked in by lowjoel, 4 years ago (diff)

Implemented an IRegistrar interface for all registrars, and an IRegisterable interface for all classes which need to be registered with IRegistrar, and migrated the Entropy and Erasure Method classes to the new interfaces. Addresses #276: Implement Eraser Registrar Interface

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