Changeset 601


Ignore:
Timestamp:
11/19/2008 1:00:48 PM (4 years ago)
Author:
lowjoel
Message:

Unoptimized default settings code. Defaults can only be declared and will be read only if the assembly is signed by the same key used to sign Manager.
Svante thinks that the code needs to be optimized, but my use cases show that the overhead is negligible (currently). Perhaps this is for a future extension?
This properly implements #32, just that it isn't the most optimised.

Location:
branches/eraser6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/DefaultPlugins/EraseGutmann.cs

    r374 r601  
    2626using System.Collections.Generic; 
    2727using System.Text; 
     28using System.Runtime.InteropServices; 
    2829 
    2930using Eraser.Manager; 
     
    3233namespace Eraser.DefaultPlugins 
    3334{ 
    34     class Gutmann : PassBasedErasureMethod 
     35    [DefaultFileErasure(1)] 
     36    [Guid("1407FC4E-FEFF-4375-B4FB-D7EFBB7E9922")] 
     37    public class Gutmann : PassBasedErasureMethod 
    3538    { 
    3639        public override string Name 
  • branches/eraser6/DefaultPlugins/EraseRandom.cs

    r348 r601  
    2323using System.Collections.Generic; 
    2424using System.Text; 
     25using System.Runtime.InteropServices; 
    2526 
    2627using Eraser.Manager; 
     
    2930namespace Eraser.DefaultPlugins 
    3031{ 
     32    [DefaultUnusedSpaceErasure(1)] 
     33    [Guid("BF8BA267-231A-4085-9BF9-204DE65A6641")] 
    3134    class Pseudorandom : PassBasedErasureMethod 
    3235    { 
  • branches/eraser6/DefaultPlugins/RNGCrypto.cs

    r348 r601  
    2323using System.Collections.Generic; 
    2424using System.Text; 
     25using System.Runtime.InteropServices; 
    2526 
    2627using Eraser.Manager; 
     
    3031namespace Eraser.DefaultPlugins 
    3132{ 
     33    [DefaultPRNG(1)] 
     34    [Guid("6BF35B8E-F37F-476e-B6B2-9994A92C3B0C")] 
    3235    public class RNGCrypto : PRNG 
    3336    { 
  • branches/eraser6/Manager/Settings.cs

    r583 r601  
    2525using System.Reflection; 
    2626using System.Runtime.InteropServices; 
     27using Eraser.Util; 
    2728 
    2829namespace Eraser.Manager 
     
    9293    } 
    9394 
     95    #region Default attributes 
     96    /// <summary> 
     97    /// Indicates that the marked class should be used as a default when no 
     98    /// settings have been set by the user. 
     99    /// </summary> 
     100    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] 
     101    public abstract class DefaultAttribute : Attribute 
     102    { 
     103        /// <summary> 
     104        /// Constructor. 
     105        /// </summary> 
     106        /// <param name="priority">The priority of the current element in terms of 
     107        /// it being the default.</param> 
     108        public DefaultAttribute(int priority) 
     109        { 
     110            Priority = priority; 
     111        } 
     112 
     113        /// <summary> 
     114        /// The priority of the default. 
     115        /// </summary> 
     116        public int Priority 
     117        { 
     118            get 
     119            { 
     120                return priority; 
     121            } 
     122            private set 
     123            { 
     124                priority = value; 
     125            } 
     126        } 
     127 
     128        private int priority; 
     129    } 
     130 
     131    /// <summary> 
     132    /// Indicates that the marked class should be used as the default file erasure 
     133    /// method. 
     134    /// </summary> 
     135    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] 
     136    public sealed class DefaultFileErasureAttribute : DefaultAttribute 
     137    { 
     138        /// <summary> 
     139        /// Constructor. 
     140        /// </summary> 
     141        /// <param name="priority">The priority of the current element in terms of 
     142        /// it being the default.</param> 
     143        public DefaultFileErasureAttribute(int priority) 
     144            : base(priority) 
     145        { 
     146        } 
     147    } 
     148 
     149    /// <summary> 
     150    /// Indicates that the marked class should be used as the default unused space 
     151    /// erasure method. 
     152    /// </summary> 
     153    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] 
     154    public sealed class DefaultUnusedSpaceErasureAttribute : DefaultAttribute 
     155    { 
     156        /// <summary> 
     157        /// Constructor. 
     158        /// </summary> 
     159        /// <param name="priority">The priority of the current element in terms of 
     160        /// it being the default.</param> 
     161        public DefaultUnusedSpaceErasureAttribute(int priority) 
     162            : base(priority) 
     163        { 
     164        } 
     165    } 
     166 
     167    /// <summary> 
     168    /// Indicates that the marked class should be used as the default PRNG. 
     169    /// </summary> 
     170    [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] 
     171    public sealed class DefaultPRNGAttribute : DefaultAttribute 
     172    { 
     173        /// <summary> 
     174        /// Constructor. 
     175        /// </summary> 
     176        /// <param name="priority">The priority of the current element in terms of 
     177        /// it being the default.</param> 
     178        public DefaultPRNGAttribute(int priority) 
     179            : base(priority) 
     180        { 
     181        } 
     182    } 
     183    #endregion 
     184 
    94185    /// <summary> 
    95186    /// Handles the settings related to the Eraser Manager. 
     
    116207            get 
    117208            { 
    118                 return settings["DefaultFileErasureMethod"] == null ?  
    119                     new Guid("1407FC4E-FEFF-4375-B4FB-D7EFBB7E9922") : 
    120                     (Guid)settings["DefaultFileErasureMethod"]; 
     209                //If the user did not define anything for this field, check all plugins 
     210                //and use the method which was declared by us to be the highest 
     211                //priority default 
     212                if (settings["DefaultFileErasureMethod"] == null) 
     213                { 
     214                    Guid result = FindHighestPriorityDefault(typeof(ErasureMethod), 
     215                        typeof(DefaultFileErasureAttribute)); 
     216                    return result == Guid.Empty ? new Guid("{1407FC4E-FEFF-4375-B4FB-D7EFBB7E9922}") : 
     217                        result; 
     218                } 
     219                else 
     220                    return (Guid)settings["DefaultFileErasureMethod"]; 
    121221            } 
    122222            set 
     
    135235            get 
    136236            { 
    137                 return settings["DefaultUnusedSpaceErasureMethod"] == null ?  
    138                     new Guid("{BF8BA267-231A-4085-9BF9-204DE65A6641}") : 
    139                     (Guid)settings["DefaultUnusedSpaceErasureMethod"]; 
     237                if (settings["DefaultUnusedSpaceErasureMethod"] == null) 
     238                { 
     239                    Guid result = FindHighestPriorityDefault(typeof(UnusedSpaceErasureMethod), 
     240                        typeof(DefaultUnusedSpaceErasureAttribute)); 
     241                    return result == Guid.Empty ? new Guid("{BF8BA267-231A-4085-9BF9-204DE65A6641}") : 
     242                        result; 
     243                } 
     244                else 
     245                    return (Guid)settings["DefaultUnusedSpaceErasureMethod"]; 
    140246            } 
    141247            set 
     
    153259            get 
    154260            { 
    155                 return settings["ActivePRNG"] == null ?  
    156                     new Guid("{6BF35B8E-F37F-476e-B6B2-9994A92C3B0C}") : 
    157                     (Guid)settings["ActivePRNG"]; 
     261                if (settings["ActivePRNG"] == null) 
     262                { 
     263                    Guid result = FindHighestPriorityDefault(typeof(PRNG), 
     264                        typeof(DefaultPRNGAttribute)); 
     265                    return result == Guid.Empty ? new Guid("{6BF35B8E-F37F-476e-B6B2-9994A92C3B0C}") : 
     266                        result; 
     267                } 
     268                else 
     269                    return (Guid)settings["ActivePRNG"]; 
    158270            } 
    159271            set 
     
    249361        } 
    250362 
     363        #region Default Attributes retrieval 
     364        private static Guid FindHighestPriorityDefault(Type superClass, 
     365            Type attributeType) 
     366        { 
     367            Plugin.Host pluginHost = ManagerLibrary.Instance.Host; 
     368            List<Plugin.PluginInstance> plugins = pluginHost.Plugins; 
     369            SortedList<int, Guid> priorities = new SortedList<int, Guid>(); 
     370 
     371            foreach (Plugin.PluginInstance plugin in plugins) 
     372            { 
     373                Type[] types = FindTypeAttributeInAssembly(plugin.Assembly, 
     374                    superClass, attributeType); 
     375 
     376                //Prioritize the types. 
     377                if (types != null) 
     378                    foreach (Type type in types) 
     379                    { 
     380                        object[] guids = 
     381                            type.GetCustomAttributes(typeof(GuidAttribute), false); 
     382                        DefaultAttribute defaultAttr = (DefaultAttribute) 
     383                            type.GetCustomAttributes(attributeType, false)[0]; 
     384 
     385                        if (guids.Length == 1) 
     386                            priorities.Add(defaultAttr.Priority, 
     387                                new Guid(((GuidAttribute)guids[0]).Value)); 
     388                    } 
     389            } 
     390 
     391            //Return the highest priority one. 
     392            if (priorities.Count > 0) 
     393                return priorities[priorities.Keys[priorities.Count - 1]]; 
     394            return Guid.Empty; 
     395        } 
     396 
     397        private static Type[] FindTypeAttributeInAssembly(Assembly assembly, Type superClass, 
     398            Type attributeType) 
     399        { 
     400            //Check whether the plugin is signed by us. 
     401            byte[] pluginKey = assembly.GetName().GetPublicKey(); 
     402            byte[] ourKey = Assembly.GetExecutingAssembly().GetName().GetPublicKey(); 
     403 
     404            //Definitely not signed by us. 
     405            if (pluginKey.Length != ourKey.Length || 
     406                !MsCorEEAPI.VerifyStrongName(assembly.Location)) 
     407                return null; 
     408 
     409            //Not by us, either 
     410            bool officialPlugin = true; 
     411            for (int i = 0, j = ourKey.Length; i != j; ++i) 
     412                if (pluginKey[i] != ourKey[i]) 
     413                    officialPlugin = false; 
     414            if (!officialPlugin) 
     415                return null; 
     416 
     417            //Yes, if we got here the plugin is signed by us. Find the 
     418            //type which inherits from ErasureMethod. 
     419            Type[] types = assembly.GetExportedTypes(); 
     420            List<Type> result = new List<Type>(); 
     421            foreach (Type type in types) 
     422            { 
     423                if (!type.IsPublic || type.IsAbstract) 
     424                    //Not interesting. 
     425                    continue; 
     426 
     427                //Try to see if this class inherits from the specified super class. 
     428                if (!type.IsSubclassOf(superClass)) 
     429                    continue; 
     430 
     431                //See if this class has the DefaultFileErasureAttribute 
     432                object[] attributes = type.GetCustomAttributes(attributeType, false); 
     433                if (attributes.Length > 0) 
     434                    result.Add(type); 
     435            } 
     436 
     437            return result.ToArray(); 
     438        } 
     439        #endregion 
     440 
    251441        /// <summary> 
    252442        /// Holds user decisions on whether the plugin will be loaded at the next 
Note: See TracChangeset for help on using the changeset viewer.