IsolatedStorage.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / IO / IsolatedStorage / IsolatedStorage.cs / 1305376 / IsolatedStorage.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
// [....]
// 
 
/*============================================================
 * 
 * Class:  IsolatedStorage
 *
 *
 * Purpose: Provides access to Persisted Application / Assembly data 
 *
 * Date:  Feb 15, 2000 
 * 
 ===========================================================*/
namespace System.IO.IsolatedStorage { 

    using System;
    using System.IO;
    using System.Text; 
    using System.Threading;
    using System.Reflection; 
    using System.Collections; 
    using System.Security;
    using System.Security.Policy; 
    using System.Security.Permissions;
    using System.Security.Cryptography;
    using System.Runtime.Serialization;
    using System.Runtime.CompilerServices; 
#if FEATURE_SERIALIZATION
    using System.Runtime.Serialization.Formatters.Binary; 
#endif // FEATURE_SERIALIZATION 
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning; 
    using Microsoft.Win32;
    using System.Diagnostics.Contracts;

 
[Serializable]
    [Flags] 
[System.Runtime.InteropServices.ComVisible(true)] 
    public enum IsolatedStorageScope
    { 
        // Dependency in native : COMIsolatedStorage.h

        None       = 0x00,
        User       = 0x01, 
#if !FEATURE_ISOSTORE_LIGHT
        Domain     = 0x02, 
        Assembly   = 0x04, 
        Roaming    = 0x08,
        Machine    = 0x10, 
#endif // FEATURE_ISOSTORE_LIGHT
        Application    = 0x20
    }
 
#if !FEATURE_ISOSTORE_LIGHT
    // not serializable 
[System.Runtime.InteropServices.ComVisible(true)] 
    public abstract class IsolatedStorage : MarshalByRefObject
    { 
        // Helper constants
        internal const IsolatedStorageScope c_Assembly =
                                   (IsolatedStorageScope.User |
                                    IsolatedStorageScope.Assembly); 

        internal const IsolatedStorageScope c_Domain = 
                                   (IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain); 

        internal const IsolatedStorageScope c_AssemblyRoaming =
                                   (IsolatedStorageScope.Roaming |
                                    IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly);
 
        internal const IsolatedStorageScope c_DomainRoaming = 
                                   (IsolatedStorageScope.Roaming |
                                    IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain);

        internal const IsolatedStorageScope c_MachineAssembly = 
                                   (IsolatedStorageScope.Machine |
                                    IsolatedStorageScope.Assembly); 
 
        internal const IsolatedStorageScope c_MachineDomain =
                                   (IsolatedStorageScope.Machine | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain);

        internal const IsolatedStorageScope c_AppUser = 
                                   (IsolatedStorageScope.Application |
                                    IsolatedStorageScope.User); 
 
        internal const IsolatedStorageScope c_AppMachine =
                                   (IsolatedStorageScope.Application | 
                                    IsolatedStorageScope.Machine);

        internal const IsolatedStorageScope c_AppUserRoaming =
                                   (IsolatedStorageScope.Roaming | 
                                    IsolatedStorageScope.Application |
                                    IsolatedStorageScope.User); 
 
#if !FEATURE_PAL
        private const String s_Publisher    = "Publisher"; 
#endif // !FEATURE_PAL
        private const String s_StrongName   = "StrongName";
        private const String s_Site         = "Site";
        private const String s_Url          = "Url"; 
        private const String s_Zone         = "Zone";
 
        private ulong   m_Quota; 
        private bool    m_ValidQuota;
 
        private Object  m_DomainIdentity;
        private Object  m_AssemIdentity;
        private Object  m_AppIdentity;
 
        private String  m_DomainName;
        private String  m_AssemName; 
        private String  m_AppName; 

        private IsolatedStorageScope m_Scope; 

        private static IsolatedStorageFilePermission s_PermDomain;
        private static IsolatedStorageFilePermission s_PermMachineDomain;
        private static IsolatedStorageFilePermission s_PermDomainRoaming; 
        private static IsolatedStorageFilePermission s_PermAssem;
        private static IsolatedStorageFilePermission s_PermMachineAssem; 
        private static IsolatedStorageFilePermission s_PermAssemRoaming; 
        private static IsolatedStorageFilePermission s_PermAppUser;
        private static IsolatedStorageFilePermission s_PermAppMachine; 
        private static IsolatedStorageFilePermission s_PermAppUserRoaming;

        private static SecurityPermission s_PermControlEvidence;
        private static PermissionSet s_PermUnrestricted; 

#if _DEBUG 
        private static bool s_fDebug = false; 
        private static int  s_iDebug = 0;
#endif 

        // This one should be a macro, expecting JIT to inline this.
        internal static bool IsRoaming(IsolatedStorageScope scope)
        { 
            return ((scope & IsolatedStorageScope.Roaming) != 0);
        } 
 
        internal bool IsRoaming()
        { 
            return ((m_Scope & IsolatedStorageScope.Roaming) != 0);
        }

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsDomain(IsolatedStorageScope scope)
        { 
            return ((scope & IsolatedStorageScope.Domain) != 0); 
        }
 
        internal bool IsDomain()
        {
            return ((m_Scope & IsolatedStorageScope.Domain) != 0);
        } 

#if false 
        // Not currently used 
        // This one should be a macro, expecting JIT to inline this.
        internal static bool IsUser(IsolatedStorageScope scope) 
        {
            return ((scope & IsolatedStorageScope.User) != 0);
        }
#endif 

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsMachine(IsolatedStorageScope scope) 
        {
            return ((scope & IsolatedStorageScope.Machine) != 0); 
        }

        internal bool IsAssembly()
        { 
            return ((m_Scope & IsolatedStorageScope.Assembly) != 0);
        } 
 

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsApp(IsolatedStorageScope scope)
        {
            return ((scope & IsolatedStorageScope.Application) != 0);
        } 

        internal bool IsApp() 
        { 
            return ((m_Scope & IsolatedStorageScope.Application) != 0);
        } 

        private String GetNameFromID(String typeID, String instanceID)
        {
            StringBuilder sb = new StringBuilder(); 
            sb.Append(typeID);
            sb.Append(SeparatorInternal); 
            sb.Append(instanceID); 

            return sb.ToString(); 
        }

        private static String GetPredefinedTypeName(Object o)
        { 
#if !FEATURE_PAL
            if (o is Publisher) 
                return s_Publisher; 
            else if (o is StrongName)
                return s_StrongName; 
#else
            if (o is StrongName)
                return s_StrongName;
#endif // !FEATURE_PAL 
            else if (o is Url)
                return s_Url; 
            else if (o is Site) 
                return s_Site;
            else if (o is Zone) 
                return s_Zone;

            return null;
        } 

#if !FEATURE_PAL 
        internal static String GetHash(Stream s) 
        {
            using (SHA1 sha1 = new SHA1CryptoServiceProvider()) 
            {
                byte[] b = sha1.ComputeHash(s);
                return Path.ToBase32StringSuitableForDirName(b);
            } 

        } 
#else 
        internal static String GetHash(Stream s)
        { 
            const int MAX_BUFFER_SIZE = 1024;
            byte[] buffer = new byte[MAX_BUFFER_SIZE];

            // 160 bits SHA1 output as defined in the Secure Hash Standard 
            const int MESSAGE_DIGEST_LENGTH = 20;
            int digestLength = 0; 
            byte[] digest = new byte[MESSAGE_DIGEST_LENGTH]; 

            IntPtr hProv = (IntPtr) 0; 
            IntPtr hHash = (IntPtr) 0;

            if( Win32Native.CryptAcquireContext(out hProv, null, null, 0, 0) == false)
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            if( Win32Native.CryptCreateHash(hProv, Win32Native.CALG_SHA1,(IntPtr) 0, 0, out hHash) == false) 
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            int bytesRead; 
            do {
                bytesRead = s.Read(buffer,0,MAX_BUFFER_SIZE);
                if (bytesRead > 0) {
                    if(Win32Native.CryptHashData(hHash, buffer, bytesRead, 0) == false) 
                    {
                        throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 
                    } 
                }
            } while (bytesRead > 0); 

            // perform a sanity check to make sure the digest size equals MESSAGE_DIGEST_LENGTH
            int fourBytes = 4;
            if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHSIZE, out digestLength, ref fourBytes, 0) == false 
                || (digestLength != MESSAGE_DIGEST_LENGTH)
              ) 
            { 
                throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
            } 

            if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHVAL, digest, ref digestLength, 0) == false)
                throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
 
            if( Win32Native.CryptDestroyHash(hHash) == false)
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 
 
            if( Win32Native.CryptReleaseContext(hProv, 0) == false )
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            return Path.ToBase32StringSuitableForDirName(digest);
        }
#endif // !FEATURE_PAL 

 
#if false 
    // This is not used.
        private static String ToBase64StringSuitableForDirName(byte[] buff) 
        {
            String s = Convert.ToBase64String(buff);
            StringBuilder sb = new StringBuilder();
 
            // Remove certain chars not suited for file names
            for (int i=0; i();
                if (o == null) 
                    o = evidence.GetHostEvidence(); 
#endif // !FEATURE_PAL
                if (o == null) 
                    o = evidence.GetHostEvidence();
                if (o == null)
                    o = evidence.GetHostEvidence();
                if (o == null) 
                    o = evidence.GetHostEvidence();
 
                if (o == null) 
                {
                    // The evidence object can have tons of other objects 
                    // creatd by the policy system. Ignore those.

                    if (fAssmDomApp == IsolatedStorageScope.Domain)
                        throw new IsolatedStorageException( 
                            Environment.GetResourceString(
                                "IsolatedStorage_DomainNoEvidence")); 
                    else if (fAssmDomApp == IsolatedStorageScope.Application) 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_ApplicationNoEvidence"));
                    else
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_AssemblyNoEvidence"));
                } 
            } 
            else
            { 
                o = evidence.GetHostEvidence(evidenceType);

                if (o == null)
                { 
                    if (fAssmDomApp == IsolatedStorageScope.Domain)
                        throw new IsolatedStorageException( 
                            Environment.GetResourceString( 
                                "IsolatedStorage_DomainNoEvidence"));
                    else if (fAssmDomApp == IsolatedStorageScope.Application) 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString(
                                "IsolatedStorage_ApplicationNoEvidence"));
                    else 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_AssemblyNoEvidence")); 
                }
            } 

            // For startup Perf, Url, Site, StrongName types don't implement
            // INormalizeForIsolatedStorage interface, instead they have
            // Normalize() method. 

            if (o is INormalizeForIsolatedStorage) 
            { 
                oNormalized = ((INormalizeForIsolatedStorage)o).Normalize();
            } 
#if !FEATURE_PAL
            else if (o is Publisher)
            {
                oNormalized = ((Publisher)o).Normalize(); 
            }
#endif // !FEATURE_PAL 
            else if (o is StrongName) 
            {
                oNormalized = ((StrongName)o).Normalize(); 
            }
            else if (o is Url)
            {
                oNormalized = ((Url)o).Normalize(); 
            }
            else if (o is Site) 
            { 
                oNormalized = ((Site)o).Normalize();
            } 
            else if (o is Zone)
            {
                oNormalized = ((Zone)o).Normalize();
            } 
            else
            { 
                oNormalized = null; 
            }
 
            return o;
        }

        [System.Security.SecurityCritical]  // auto-generated 
        private static void DemandPermission(IsolatedStorageScope scope)
        { 
            IsolatedStorageFilePermission ip = null; 

            // Ok to create more than one instnace of s_PermXXX, the last one 
            // will be shared. No need to synchronize.

            // First check for permissions
 
            switch (scope)
            { 
            case c_Domain: 

                if (s_PermDomain == null) 
                    s_PermDomain = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.DomainIsolationByUser,
                        0, false);
                ip = s_PermDomain; 
                break;
 
            case c_Assembly: 
                if (s_PermAssem == null)
                    s_PermAssem = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.AssemblyIsolationByUser,
                        0, false);
                ip = s_PermAssem;
                break; 

            case c_DomainRoaming: 
                if (s_PermDomainRoaming == null) 
                    s_PermDomainRoaming = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.DomainIsolationByRoamingUser, 
                        0, false);
                ip = s_PermDomainRoaming;
                break;
 
            case c_AssemblyRoaming:
                if (s_PermAssemRoaming == null) 
                    s_PermAssemRoaming = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.AssemblyIsolationByRoamingUser,
                        0, false); 
                ip = s_PermAssemRoaming;
                break;
            case c_MachineDomain:
 
                if (s_PermMachineDomain == null)
                    s_PermMachineDomain = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.DomainIsolationByMachine, 
                        0, false);
                ip = s_PermMachineDomain; 
                break;

            case c_MachineAssembly:
                if (s_PermMachineAssem == null) 
                    s_PermMachineAssem = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.AssemblyIsolationByMachine, 
                        0, false); 
                ip = s_PermMachineAssem;
                break; 

            case c_AppUser:
                if (s_PermAppUser == null)
                    s_PermAppUser = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.ApplicationIsolationByUser,
                        0, false); 
                ip = s_PermAppUser; 
                break;
 
            case c_AppMachine:
                if (s_PermAppMachine == null)
                    s_PermAppMachine = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.ApplicationIsolationByMachine, 
                        0, false);
                ip = s_PermAppMachine; 
                break; 

            case c_AppUserRoaming: 
                if (s_PermAppUserRoaming== null)
                    s_PermAppUserRoaming= new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.ApplicationIsolationByRoamingUser,
                        0, false); 
                ip = s_PermAppUserRoaming;
                break; 
 
#if _DEBUG
            default: 
                Contract.Assert(false, "Invalid scope");
                break;
#endif
            } 

            ip.Demand(); 
        } 

        internal static void VerifyScope(IsolatedStorageScope scope) 
        {
            // The only valid ones are the ones that have a helper constant defined above (c_*)

            if ((scope == c_Domain) || (scope == c_Assembly) || 
                (scope == c_DomainRoaming) || (scope == c_AssemblyRoaming) ||
                (scope == c_MachineDomain) || (scope == c_MachineAssembly) || 
                (scope == c_AppUser) || (scope == c_AppMachine) || 
                (scope == c_AppUserRoaming))
                return; 

            throw new ArgumentException(
                    Environment.GetResourceString(
                        "IsolatedStorage_Scope_Invalid")); 
        }
 
 
        [System.Security.SecurityCritical]
        internal virtual void SetQuota(PermissionSet psAllowed, PermissionSet psDenied) 
        {
            IsolatedStoragePermission ispAllowed, ispDenied;

            ispAllowed = GetPermission(psAllowed); 

            m_Quota = 0; 
 
            if (ispAllowed != null)
            { 
                if (ispAllowed.IsUnrestricted())
                    m_Quota = Int64.MaxValue;
                else
                    m_Quota = (ulong) ispAllowed.UserQuota; 
            }
 
            if (psDenied != null) 
            {
                ispDenied = GetPermission(psDenied); 

                if (ispDenied != null)
                {
                    if (ispDenied.IsUnrestricted()) 
                    {
                        m_Quota = 0; 
                    } 
                    else
                    { 
                        ulong denied = (ulong) ispDenied.UserQuota;

                        if (denied > m_Quota)
                            m_Quota = 0; 
                        else
                            m_Quota -= denied; 
                    } 
                }
            } 

            m_ValidQuota = true;

#if _DEBUG 
            if (s_fDebug)
            { 
                if (s_iDebug >= 1) { 
                    if (psAllowed != null)
                        Console.WriteLine("Allowed PS : " + psAllowed); 
                    if (psDenied != null)
                        Console.WriteLine("Denied PS : " + psDenied);
                }
            } 
#endif
        } 
 
#if _DEBUG
        private static void DebugLog(Object o, MemoryStream ms) 
        {
            if (s_fDebug)
            {
                if (s_iDebug >= 1) 
                    Console.WriteLine(o.ToString());
 
                if (s_iDebug >= 10) 
                {
                    byte[] p = ms.GetBuffer(); 

                    for (int _i=0; _i[....]
// 
 
/*============================================================
 * 
 * Class:  IsolatedStorage
 *
 *
 * Purpose: Provides access to Persisted Application / Assembly data 
 *
 * Date:  Feb 15, 2000 
 * 
 ===========================================================*/
namespace System.IO.IsolatedStorage { 

    using System;
    using System.IO;
    using System.Text; 
    using System.Threading;
    using System.Reflection; 
    using System.Collections; 
    using System.Security;
    using System.Security.Policy; 
    using System.Security.Permissions;
    using System.Security.Cryptography;
    using System.Runtime.Serialization;
    using System.Runtime.CompilerServices; 
#if FEATURE_SERIALIZATION
    using System.Runtime.Serialization.Formatters.Binary; 
#endif // FEATURE_SERIALIZATION 
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning; 
    using Microsoft.Win32;
    using System.Diagnostics.Contracts;

 
[Serializable]
    [Flags] 
[System.Runtime.InteropServices.ComVisible(true)] 
    public enum IsolatedStorageScope
    { 
        // Dependency in native : COMIsolatedStorage.h

        None       = 0x00,
        User       = 0x01, 
#if !FEATURE_ISOSTORE_LIGHT
        Domain     = 0x02, 
        Assembly   = 0x04, 
        Roaming    = 0x08,
        Machine    = 0x10, 
#endif // FEATURE_ISOSTORE_LIGHT
        Application    = 0x20
    }
 
#if !FEATURE_ISOSTORE_LIGHT
    // not serializable 
[System.Runtime.InteropServices.ComVisible(true)] 
    public abstract class IsolatedStorage : MarshalByRefObject
    { 
        // Helper constants
        internal const IsolatedStorageScope c_Assembly =
                                   (IsolatedStorageScope.User |
                                    IsolatedStorageScope.Assembly); 

        internal const IsolatedStorageScope c_Domain = 
                                   (IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain); 

        internal const IsolatedStorageScope c_AssemblyRoaming =
                                   (IsolatedStorageScope.Roaming |
                                    IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly);
 
        internal const IsolatedStorageScope c_DomainRoaming = 
                                   (IsolatedStorageScope.Roaming |
                                    IsolatedStorageScope.User | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain);

        internal const IsolatedStorageScope c_MachineAssembly = 
                                   (IsolatedStorageScope.Machine |
                                    IsolatedStorageScope.Assembly); 
 
        internal const IsolatedStorageScope c_MachineDomain =
                                   (IsolatedStorageScope.Machine | 
                                    IsolatedStorageScope.Assembly |
                                    IsolatedStorageScope.Domain);

        internal const IsolatedStorageScope c_AppUser = 
                                   (IsolatedStorageScope.Application |
                                    IsolatedStorageScope.User); 
 
        internal const IsolatedStorageScope c_AppMachine =
                                   (IsolatedStorageScope.Application | 
                                    IsolatedStorageScope.Machine);

        internal const IsolatedStorageScope c_AppUserRoaming =
                                   (IsolatedStorageScope.Roaming | 
                                    IsolatedStorageScope.Application |
                                    IsolatedStorageScope.User); 
 
#if !FEATURE_PAL
        private const String s_Publisher    = "Publisher"; 
#endif // !FEATURE_PAL
        private const String s_StrongName   = "StrongName";
        private const String s_Site         = "Site";
        private const String s_Url          = "Url"; 
        private const String s_Zone         = "Zone";
 
        private ulong   m_Quota; 
        private bool    m_ValidQuota;
 
        private Object  m_DomainIdentity;
        private Object  m_AssemIdentity;
        private Object  m_AppIdentity;
 
        private String  m_DomainName;
        private String  m_AssemName; 
        private String  m_AppName; 

        private IsolatedStorageScope m_Scope; 

        private static IsolatedStorageFilePermission s_PermDomain;
        private static IsolatedStorageFilePermission s_PermMachineDomain;
        private static IsolatedStorageFilePermission s_PermDomainRoaming; 
        private static IsolatedStorageFilePermission s_PermAssem;
        private static IsolatedStorageFilePermission s_PermMachineAssem; 
        private static IsolatedStorageFilePermission s_PermAssemRoaming; 
        private static IsolatedStorageFilePermission s_PermAppUser;
        private static IsolatedStorageFilePermission s_PermAppMachine; 
        private static IsolatedStorageFilePermission s_PermAppUserRoaming;

        private static SecurityPermission s_PermControlEvidence;
        private static PermissionSet s_PermUnrestricted; 

#if _DEBUG 
        private static bool s_fDebug = false; 
        private static int  s_iDebug = 0;
#endif 

        // This one should be a macro, expecting JIT to inline this.
        internal static bool IsRoaming(IsolatedStorageScope scope)
        { 
            return ((scope & IsolatedStorageScope.Roaming) != 0);
        } 
 
        internal bool IsRoaming()
        { 
            return ((m_Scope & IsolatedStorageScope.Roaming) != 0);
        }

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsDomain(IsolatedStorageScope scope)
        { 
            return ((scope & IsolatedStorageScope.Domain) != 0); 
        }
 
        internal bool IsDomain()
        {
            return ((m_Scope & IsolatedStorageScope.Domain) != 0);
        } 

#if false 
        // Not currently used 
        // This one should be a macro, expecting JIT to inline this.
        internal static bool IsUser(IsolatedStorageScope scope) 
        {
            return ((scope & IsolatedStorageScope.User) != 0);
        }
#endif 

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsMachine(IsolatedStorageScope scope) 
        {
            return ((scope & IsolatedStorageScope.Machine) != 0); 
        }

        internal bool IsAssembly()
        { 
            return ((m_Scope & IsolatedStorageScope.Assembly) != 0);
        } 
 

        // This one should be a macro, expecting JIT to inline this. 
        internal static bool IsApp(IsolatedStorageScope scope)
        {
            return ((scope & IsolatedStorageScope.Application) != 0);
        } 

        internal bool IsApp() 
        { 
            return ((m_Scope & IsolatedStorageScope.Application) != 0);
        } 

        private String GetNameFromID(String typeID, String instanceID)
        {
            StringBuilder sb = new StringBuilder(); 
            sb.Append(typeID);
            sb.Append(SeparatorInternal); 
            sb.Append(instanceID); 

            return sb.ToString(); 
        }

        private static String GetPredefinedTypeName(Object o)
        { 
#if !FEATURE_PAL
            if (o is Publisher) 
                return s_Publisher; 
            else if (o is StrongName)
                return s_StrongName; 
#else
            if (o is StrongName)
                return s_StrongName;
#endif // !FEATURE_PAL 
            else if (o is Url)
                return s_Url; 
            else if (o is Site) 
                return s_Site;
            else if (o is Zone) 
                return s_Zone;

            return null;
        } 

#if !FEATURE_PAL 
        internal static String GetHash(Stream s) 
        {
            using (SHA1 sha1 = new SHA1CryptoServiceProvider()) 
            {
                byte[] b = sha1.ComputeHash(s);
                return Path.ToBase32StringSuitableForDirName(b);
            } 

        } 
#else 
        internal static String GetHash(Stream s)
        { 
            const int MAX_BUFFER_SIZE = 1024;
            byte[] buffer = new byte[MAX_BUFFER_SIZE];

            // 160 bits SHA1 output as defined in the Secure Hash Standard 
            const int MESSAGE_DIGEST_LENGTH = 20;
            int digestLength = 0; 
            byte[] digest = new byte[MESSAGE_DIGEST_LENGTH]; 

            IntPtr hProv = (IntPtr) 0; 
            IntPtr hHash = (IntPtr) 0;

            if( Win32Native.CryptAcquireContext(out hProv, null, null, 0, 0) == false)
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            if( Win32Native.CryptCreateHash(hProv, Win32Native.CALG_SHA1,(IntPtr) 0, 0, out hHash) == false) 
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            int bytesRead; 
            do {
                bytesRead = s.Read(buffer,0,MAX_BUFFER_SIZE);
                if (bytesRead > 0) {
                    if(Win32Native.CryptHashData(hHash, buffer, bytesRead, 0) == false) 
                    {
                        throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 
                    } 
                }
            } while (bytesRead > 0); 

            // perform a sanity check to make sure the digest size equals MESSAGE_DIGEST_LENGTH
            int fourBytes = 4;
            if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHSIZE, out digestLength, ref fourBytes, 0) == false 
                || (digestLength != MESSAGE_DIGEST_LENGTH)
              ) 
            { 
                throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
            } 

            if( Win32Native.CryptGetHashParam(hHash, Win32Native.HP_HASHVAL, digest, ref digestLength, 0) == false)
                throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception"));
 
            if( Win32Native.CryptDestroyHash(hHash) == false)
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 
 
            if( Win32Native.CryptReleaseContext(hProv, 0) == false )
               throw new IsolatedStorageException(Environment.GetResourceString("IsolatedStorage_Exception")); 

            return Path.ToBase32StringSuitableForDirName(digest);
        }
#endif // !FEATURE_PAL 

 
#if false 
    // This is not used.
        private static String ToBase64StringSuitableForDirName(byte[] buff) 
        {
            String s = Convert.ToBase64String(buff);
            StringBuilder sb = new StringBuilder();
 
            // Remove certain chars not suited for file names
            for (int i=0; i();
                if (o == null) 
                    o = evidence.GetHostEvidence(); 
#endif // !FEATURE_PAL
                if (o == null) 
                    o = evidence.GetHostEvidence();
                if (o == null)
                    o = evidence.GetHostEvidence();
                if (o == null) 
                    o = evidence.GetHostEvidence();
 
                if (o == null) 
                {
                    // The evidence object can have tons of other objects 
                    // creatd by the policy system. Ignore those.

                    if (fAssmDomApp == IsolatedStorageScope.Domain)
                        throw new IsolatedStorageException( 
                            Environment.GetResourceString(
                                "IsolatedStorage_DomainNoEvidence")); 
                    else if (fAssmDomApp == IsolatedStorageScope.Application) 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_ApplicationNoEvidence"));
                    else
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_AssemblyNoEvidence"));
                } 
            } 
            else
            { 
                o = evidence.GetHostEvidence(evidenceType);

                if (o == null)
                { 
                    if (fAssmDomApp == IsolatedStorageScope.Domain)
                        throw new IsolatedStorageException( 
                            Environment.GetResourceString( 
                                "IsolatedStorage_DomainNoEvidence"));
                    else if (fAssmDomApp == IsolatedStorageScope.Application) 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString(
                                "IsolatedStorage_ApplicationNoEvidence"));
                    else 
                        throw new IsolatedStorageException(
                            Environment.GetResourceString( 
                                "IsolatedStorage_AssemblyNoEvidence")); 
                }
            } 

            // For startup Perf, Url, Site, StrongName types don't implement
            // INormalizeForIsolatedStorage interface, instead they have
            // Normalize() method. 

            if (o is INormalizeForIsolatedStorage) 
            { 
                oNormalized = ((INormalizeForIsolatedStorage)o).Normalize();
            } 
#if !FEATURE_PAL
            else if (o is Publisher)
            {
                oNormalized = ((Publisher)o).Normalize(); 
            }
#endif // !FEATURE_PAL 
            else if (o is StrongName) 
            {
                oNormalized = ((StrongName)o).Normalize(); 
            }
            else if (o is Url)
            {
                oNormalized = ((Url)o).Normalize(); 
            }
            else if (o is Site) 
            { 
                oNormalized = ((Site)o).Normalize();
            } 
            else if (o is Zone)
            {
                oNormalized = ((Zone)o).Normalize();
            } 
            else
            { 
                oNormalized = null; 
            }
 
            return o;
        }

        [System.Security.SecurityCritical]  // auto-generated 
        private static void DemandPermission(IsolatedStorageScope scope)
        { 
            IsolatedStorageFilePermission ip = null; 

            // Ok to create more than one instnace of s_PermXXX, the last one 
            // will be shared. No need to synchronize.

            // First check for permissions
 
            switch (scope)
            { 
            case c_Domain: 

                if (s_PermDomain == null) 
                    s_PermDomain = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.DomainIsolationByUser,
                        0, false);
                ip = s_PermDomain; 
                break;
 
            case c_Assembly: 
                if (s_PermAssem == null)
                    s_PermAssem = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.AssemblyIsolationByUser,
                        0, false);
                ip = s_PermAssem;
                break; 

            case c_DomainRoaming: 
                if (s_PermDomainRoaming == null) 
                    s_PermDomainRoaming = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.DomainIsolationByRoamingUser, 
                        0, false);
                ip = s_PermDomainRoaming;
                break;
 
            case c_AssemblyRoaming:
                if (s_PermAssemRoaming == null) 
                    s_PermAssemRoaming = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.AssemblyIsolationByRoamingUser,
                        0, false); 
                ip = s_PermAssemRoaming;
                break;
            case c_MachineDomain:
 
                if (s_PermMachineDomain == null)
                    s_PermMachineDomain = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.DomainIsolationByMachine, 
                        0, false);
                ip = s_PermMachineDomain; 
                break;

            case c_MachineAssembly:
                if (s_PermMachineAssem == null) 
                    s_PermMachineAssem = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.AssemblyIsolationByMachine, 
                        0, false); 
                ip = s_PermMachineAssem;
                break; 

            case c_AppUser:
                if (s_PermAppUser == null)
                    s_PermAppUser = new IsolatedStorageFilePermission( 
                        IsolatedStorageContainment.ApplicationIsolationByUser,
                        0, false); 
                ip = s_PermAppUser; 
                break;
 
            case c_AppMachine:
                if (s_PermAppMachine == null)
                    s_PermAppMachine = new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.ApplicationIsolationByMachine, 
                        0, false);
                ip = s_PermAppMachine; 
                break; 

            case c_AppUserRoaming: 
                if (s_PermAppUserRoaming== null)
                    s_PermAppUserRoaming= new IsolatedStorageFilePermission(
                        IsolatedStorageContainment.ApplicationIsolationByRoamingUser,
                        0, false); 
                ip = s_PermAppUserRoaming;
                break; 
 
#if _DEBUG
            default: 
                Contract.Assert(false, "Invalid scope");
                break;
#endif
            } 

            ip.Demand(); 
        } 

        internal static void VerifyScope(IsolatedStorageScope scope) 
        {
            // The only valid ones are the ones that have a helper constant defined above (c_*)

            if ((scope == c_Domain) || (scope == c_Assembly) || 
                (scope == c_DomainRoaming) || (scope == c_AssemblyRoaming) ||
                (scope == c_MachineDomain) || (scope == c_MachineAssembly) || 
                (scope == c_AppUser) || (scope == c_AppMachine) || 
                (scope == c_AppUserRoaming))
                return; 

            throw new ArgumentException(
                    Environment.GetResourceString(
                        "IsolatedStorage_Scope_Invalid")); 
        }
 
 
        [System.Security.SecurityCritical]
        internal virtual void SetQuota(PermissionSet psAllowed, PermissionSet psDenied) 
        {
            IsolatedStoragePermission ispAllowed, ispDenied;

            ispAllowed = GetPermission(psAllowed); 

            m_Quota = 0; 
 
            if (ispAllowed != null)
            { 
                if (ispAllowed.IsUnrestricted())
                    m_Quota = Int64.MaxValue;
                else
                    m_Quota = (ulong) ispAllowed.UserQuota; 
            }
 
            if (psDenied != null) 
            {
                ispDenied = GetPermission(psDenied); 

                if (ispDenied != null)
                {
                    if (ispDenied.IsUnrestricted()) 
                    {
                        m_Quota = 0; 
                    } 
                    else
                    { 
                        ulong denied = (ulong) ispDenied.UserQuota;

                        if (denied > m_Quota)
                            m_Quota = 0; 
                        else
                            m_Quota -= denied; 
                    } 
                }
            } 

            m_ValidQuota = true;

#if _DEBUG 
            if (s_fDebug)
            { 
                if (s_iDebug >= 1) { 
                    if (psAllowed != null)
                        Console.WriteLine("Allowed PS : " + psAllowed); 
                    if (psDenied != null)
                        Console.WriteLine("Denied PS : " + psDenied);
                }
            } 
#endif
        } 
 
#if _DEBUG
        private static void DebugLog(Object o, MemoryStream ms) 
        {
            if (s_fDebug)
            {
                if (s_iDebug >= 1) 
                    Console.WriteLine(o.ToString());
 
                if (s_iDebug >= 10) 
                {
                    byte[] p = ms.GetBuffer(); 

                    for (int _i=0; _i

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK