Exemplo n.º 1
0
        /// <summary>
        /// Get the file from disk or from the storage cache.
        /// </summary>
        /// <param name="filePath">Full file path</param>
        /// <param name="access">If the file is opened for reading, file content is loaded</param>
        /// <param name="st">Serialization type for SAVING</param>
        /// <param name="password">File password</param>
        /// <param name="useCache"></param>
        /// <returns>Msnpsharp contact list file</returns>
        /// <remarks>This method is thread safe</remarks>
        public static MclFile Open(string filePath, FileAccess access, MclSerialization st, string password, bool useCache)
        {
            if (useCache)
            {
                if (storage.ContainsKey(filePath))
                {
                    lock (SyncObject)
                    {
                        if (storage[filePath].Refresh())
                        {
                            storage[filePath] = new MclInfo(new MclFile(filePath, st, access, password));
                        }
                    }
                }
                else
                {
                    lock (SyncObject)
                    {
                        if (!storage.ContainsKey(filePath))
                        {
                            storage[filePath] = new MclInfo(new MclFile(filePath, st, access, password));
                        }
                    }
                }

                return(storage[filePath].File);
            }
            else
            {
                return(new MclFile(filePath, st, access, password));
            }
        }
Exemplo n.º 2
0
        public static DeltasList LoadFromFile(string filename, MclSerialization st, string password, bool useCache)
        {
            DeltasList deltas = (DeltasList)LoadFromFile(filename, st, typeof(DeltasList), password, useCache);

            deltas.InitializeCacheKeys();
            return(deltas);
        }
Exemplo n.º 3
0
        public MclFile(string filename, MclSerialization st, FileAccess access, string password)
        {
            fileName         = filename;
            mclSerialization = st;

            if (!String.IsNullOrEmpty(password))
            {
                using (SHA256Managed sha256 = new SHA256Managed())
                    sha256Password = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
            }

            if ((access & FileAccess.Read) == FileAccess.Read)
            {
                xmlData = GetStruct().content;
            }
        }
Exemplo n.º 4
0
        protected static MCLSerializer LoadFromFile(string filename, MclSerialization st, Type targettype, string password, bool useCache)
        {
            int           beginTick = Environment.TickCount;
            MCLSerializer ret       = (MCLSerializer)Activator.CreateInstance(targettype);


            if (Settings.NoSave == false && File.Exists(filename))
            {
                MclFile file = MclFile.Open(filename, FileAccess.Read, st, password, useCache);

                int deserializeTick = Environment.TickCount;

                if (file.Content != null)
                {
                    using (MemoryStream ms = new MemoryStream(file.Content))
                    {
                        try
                        {
                            ret = (MCLSerializer) new XmlSerializer(targettype).Deserialize(ms);
                        }
                        catch (InvalidOperationException)
                        {
                            // Deserialize error: XML struct changed, so create a empty mcl serializer.
                            ret = (MCLSerializer)Activator.CreateInstance(targettype);
                        }
                    }
                }

                int deserializeTickConsume = Environment.TickCount - deserializeTick;
                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Deserialize time (by ticks): " + deserializeTickConsume);
            }

            ret.SerializationType = st;
            ret.FileName          = filename;
            //ret.NSMessageHandler = handler;
            ret.UseCache = useCache;
            ret.password = password;

            int tickConsume = Environment.TickCount - beginTick;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Total loading time (by ticks): " + tickConsume + "\r\n");

            return(ret);
        }
Exemplo n.º 5
0
 public static XMLContactList LoadFromFile(string filename, MclSerialization st, NSMessageHandler nsHandler, bool useCache)
 {
     XMLContactList contactlist = (XMLContactList)LoadFromFile(filename, st, typeof(XMLContactList), nsHandler.Credentials.Password, useCache);
     contactlist.NSMessageHandler = nsHandler;
     return contactlist;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        static Settings()
        {
            isMono = (null != Type.GetType("Mono.Runtime")); // http://www.mono-project.com/FAQ:_Technical

            TraceSwitch.Level = TraceLevel.Verbose;

            serializationType = MclSerialization.Compression | MclSerialization.Cryptography;

            enableGzipCompressionForWebServices = (isMono == false);
            #if DEBUG
            TraceSoap = true;
            #endif
        }
Exemplo n.º 7
0
        /// <summary>
        /// Get the file from disk or from the storage cache.
        /// </summary>
        /// <param name="filePath">Full file path</param>
        /// <param name="access">If the file is opened for reading, file content is loaded</param>
        /// <param name="st">Serialization type for SAVING</param>
        /// <param name="password">File password</param>
        /// <param name="useCache"></param>
        /// <returns>Msnpsharp contact list file</returns>
        /// <remarks>This method is thread safe</remarks>
        public static MclFile Open(string filePath, FileAccess access, MclSerialization st, string password, bool useCache)
        {
            if (useCache)
            {
                if (storage.ContainsKey(filePath))
                {
                    lock (SyncObject)
                    {
                        if (storage[filePath].Refresh())
                        {
                            storage[filePath] = new MclInfo(new MclFile(filePath, st, access, password));
                        }
                    }
                }
                else
                {
                    lock (SyncObject)
                    {
                        if (!storage.ContainsKey(filePath))
                        {
                            storage[filePath] = new MclInfo(new MclFile(filePath, st, access, password));
                        }
                    }
                }

                return storage[filePath].File;
            }
            else
            {
                return new MclFile(filePath, st, access, password);
            }
        }
Exemplo n.º 8
0
        public MclFile(string filename, MclSerialization st, FileAccess access, string password)
        {
            fileName = filename;
            mclSerialization = st;

            if (!String.IsNullOrEmpty(password))
            {
                using (SHA256Managed sha256 = new SHA256Managed())
                    sha256Password = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
            }

            if ((access & FileAccess.Read) == FileAccess.Read)
                xmlData = GetStruct().content;
        }
Exemplo n.º 9
0
        protected static MCLSerializer LoadFromFile(string filename, MclSerialization st, Type targettype, string password, bool useCache)
        {
            int beginTick = Environment.TickCount;
            MCLSerializer ret = (MCLSerializer)Activator.CreateInstance(targettype);

            if (Settings.NoSave == false && File.Exists(filename))
            {
                MclFile file = MclFile.Open(filename, FileAccess.Read, st, password, useCache);

                int deserializeTick = Environment.TickCount;

                if (file.Content != null)
                {
                    using (MemoryStream ms = new MemoryStream(file.Content))
                    {
                        try
                        {
                            ret = (MCLSerializer)new XmlSerializer(targettype).Deserialize(ms);
                        }
                        catch (InvalidOperationException)
                        {
                            // Deserialize error: XML struct changed, so create a empty mcl serializer.
                            ret = (MCLSerializer)Activator.CreateInstance(targettype);
                        }
                    }
                }

                int deserializeTickConsume = Environment.TickCount - deserializeTick;
                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Deserialize time (by ticks): " + deserializeTickConsume);
            }

            ret.SerializationType = st;
            ret.FileName = filename;
            //ret.NSMessageHandler = handler;
            ret.UseCache = useCache;
            ret.password = password;

            int tickConsume = Environment.TickCount - beginTick;
            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Total loading time (by ticks): " + tickConsume + "\r\n");

            return ret;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Decompress/Decyrpt the file if the serialization type is not XML.
        /// </summary>
        /// <returns></returns>
        private MclFileStruct GetStruct()
        {
            MclFileStruct mclfile = new MclFileStruct();

            if (File.Exists(fileName))
            {
                MemoryStream ms = new MemoryStream();
                FileStream   fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
                try
                {
                    byte[] first3 = new byte[MclBytes.Length];
                    if (MclBytes.Length == fs.Read(first3, 0, first3.Length))
                    {
                        MclSerialization st = MclSerialization.None;

                        if (first3[0] == MclBytes[0] && first3[1] == MclBytes[1] && first3[2] == MclBytes[2])
                        {
                            st = MclSerialization.Compression;
                        }
                        else if (first3[0] == MpwBytes[0] && first3[1] == MpwBytes[1] && first3[2] == MpwBytes[2])
                        {
                            st = MclSerialization.Cryptography;
                        }
                        else if (first3[0] == McpBytes[0] && first3[1] == McpBytes[1] && first3[2] == McpBytes[2])
                        {
                            st = MclSerialization.Compression | MclSerialization.Cryptography;
                        }
                        else
                        {
                            st = MclSerialization.None;
                            ms.Write(first3, 0, first3.Length);
                        }

                        int    read;
                        byte[] tmp = new byte[16384];
                        while ((read = fs.Read(tmp, 0, tmp.Length)) > 0)
                        {
                            ms.Write(tmp, 0, read);
                        }

                        switch (st)
                        {
                        case MclSerialization.None:
                            mclfile.content = ms.ToArray();
                            break;

                        case MclSerialization.Compression:
                            mclfile.content = Decompress(ms.ToArray());
                            break;

                        case MclSerialization.Cryptography:
                            mclfile.content = Decyrpt(ms.ToArray(), sha256Password);
                            break;

                        case MclSerialization.Compression | MclSerialization.Cryptography:
                            byte[] compressed = Decyrpt(ms.ToArray(), sha256Password);
                            mclfile.content = Decompress(compressed);
                            break;
                        }
                    }
                }
                catch (Exception exception)
                {
                    Trace.WriteLineIf(Settings.TraceSwitch.TraceError, exception.Message, GetType().Name);
                    return(new MclFileStruct());
                }
                finally
                {
                    fs.Close();
                    ms.Close();
                }
            }
            return(mclfile);
        }
Exemplo n.º 11
0
 public static DeltasList LoadFromFile(string filename, MclSerialization st, string password, bool useCache)
 {
     DeltasList deltas = (DeltasList)LoadFromFile(filename, st, typeof(DeltasList), password, useCache);
     deltas.InitializeCacheKeys();
     return deltas;
 }