IsolatedStorage.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / clr / src / BCL / System / IO / IsolatedStorage / IsolatedStorage.cs / 1 / 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;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.InteropServices; 
    using Microsoft.Win32;
 
    [Flags, Serializable] 
[System.Runtime.InteropServices.ComVisible(true)]
    public enum IsolatedStorageScope 
    {
        // Dependency in native : COMIsolatedStorage.h

        None       = 0x00, 
        User       = 0x01,
        Domain     = 0x02, 
        Assembly   = 0x04, 
        Roaming    = 0x08,
        Machine    = 0x10, 
        Application    = 0x20
    }

    // 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 static Char[] s_Base32Char   = {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
                'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 
                'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z', '0', '1', '2', '3', '4', '5'}; 

        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_PermReflection;
        private static PermissionSet s_PermUnrestricted; 
        private static PermissionSet s_PermExecution;

#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) 
        {
            SHA1Managed sha1 = new SHA1Managed(); 
            byte[] b = sha1.ComputeHash(s); 
            return 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 ToBase32StringSuitableForDirName(digest);
        } 
#endif // !FEATURE_PAL 

        internal static String ToBase32StringSuitableForDirName(byte[] buff) 
        {
            // This routine is optimised to be used with buffs of length 20
            BCLDebug.Assert(((buff.Length % 5) == 0), "Unexpected hash length");
 
#if _DEBUG
            if (s_fDebug) 
            { 
                if (s_iDebug >= 10)
                { 
                    Console.Write("Stream : ");
                    for (int j = 0; j> 5) | 
                        ((b3 & 0x60) >> 2))]);

                sb.Append(s_Base32Char[(
                        ((b1 & 0xE0) >> 5) | 
                        ((b4 & 0x60) >> 2))]);
 
                // Consume 3 MSB bits of b2, 1 MSB bit of b3, b4 

                b2 >>= 5; 

                BCLDebug.Assert(((b2 & 0xF8) == 0), "Unexpected set bits");

                if ((b3 & 0x80) != 0) 
                    b2 |= 0x08;
                if ((b4 & 0x80) != 0) 
                    b2 |= 0x10; 

                sb.Append(s_Base32Char[b2]); 

            } while (i < l);

#if _DEBUG 
            if (s_fDebug)
            { 
                if (s_iDebug >= 10) 
                    Console.WriteLine("Hash : " + sb.ToString());
            } 
#endif
            return sb.ToString();
        }
 
#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 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 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 ToBase32StringSuitableForDirName(digest);
        } 
#endif // !FEATURE_PAL 

        internal static String ToBase32StringSuitableForDirName(byte[] buff) 
        {
            // This routine is optimised to be used with buffs of length 20
            BCLDebug.Assert(((buff.Length % 5) == 0), "Unexpected hash length");
 
#if _DEBUG
            if (s_fDebug) 
            { 
                if (s_iDebug >= 10)
                { 
                    Console.Write("Stream : ");
                    for (int j = 0; j> 5) | 
                        ((b3 & 0x60) >> 2))]);

                sb.Append(s_Base32Char[(
                        ((b1 & 0xE0) >> 5) | 
                        ((b4 & 0x60) >> 2))]);
 
                // Consume 3 MSB bits of b2, 1 MSB bit of b3, b4 

                b2 >>= 5; 

                BCLDebug.Assert(((b2 & 0xF8) == 0), "Unexpected set bits");

                if ((b3 & 0x80) != 0) 
                    b2 |= 0x08;
                if ((b4 & 0x80) != 0) 
                    b2 |= 0x10; 

                sb.Append(s_Base32Char[b2]); 

            } while (i < l);

#if _DEBUG 
            if (s_fDebug)
            { 
                if (s_iDebug >= 10) 
                    Console.WriteLine("Hash : " + sb.ToString());
            } 
#endif
            return sb.ToString();
        }
 
#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 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