Esempio n. 1
0
        /// <summary>
        /// Locks, reads the usage count XML serialized file and decrypts it. If no data found, the lock is released
        /// </summary>
        /// <returns></returns>
        public bool Read()
        {
            try
            {
                try
                {
                    _memoryStream = Locker.ReadDecryptFile(ref _fileStream, _usageCountPath1, _decryptionPassword, false);
                }
                catch (InvalidOperationException) // other exception will be thrown to the caller
                {
                    _memoryStream = Locker.ReadDecryptFile(ref _fileStream, _usageCountPath2, _decryptionPassword, false);
                }
            }
            catch (Exception ex)
            {
                Locker.ClearFileStream(ref _fileStream);
                throw ex;
            }

            if (_memoryStream == null)
            {
                Locker.ClearFileStream(ref _fileStream);
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
 private void ReleaseAggregatedFiles()
 {
     Locker.ClearFileStream(ref _dateImpressionsPerAssetStream);
     Locker.ClearFileStream(ref _dateClicksPerAssetStream);
     Locker.ClearFileStream(ref _advertChannelImpressionProportionsStream);
     Locker.ClearFileStream(ref _advertChannelClickProportionsStream);
 }
Esempio n. 3
0
        /// <summary>
        /// Locks and loads Aggregated file if it exists.
        /// </summary>
        /// <param name="path">Path to the aggregate file</param>
        /// <param name="fs">FileStream reference</param>
        /// <param name="decryptionPassword">password to use to decrypt the file</param>
        /// <returns>a string with the flat aggragated logs if there are any or an empty string if there aren't</returns>
        public static string LoadAggregatedLogFile(string path, ref FileStream fs, string decryptionPassword)
        {
            string existingLogs = "";

            MemoryStream memoryStream = Locker.ReadDecryptFile(ref fs, path, decryptionPassword, false, true);

            if (memoryStream != null)
            {
                try
                {
                    existingLogs = GetExistingString(memoryStream);
                }
                catch (Exception ex)
                {
                    Locker.ClearFileStream(ref fs);
                    throw ex;
                }
                finally
                {
                    memoryStream.Close();
                    memoryStream.Dispose();
                }
            }

            return(existingLogs);
        }
Esempio n. 4
0
 private void ReleaseRawFiles()
 {
     Locker.ClearFileStream(ref _contentClickLogStream);
     Locker.ClearFileStream(ref _advertClickLogStream);
     Locker.ClearFileStream(ref _contentImpressionLogStream);
     Locker.ClearFileStream(ref _advertImpressionLogStream);
 }
Esempio n. 5
0
        private static Queue <string> ReadExistingLogs(ref FileStream fileStream,
                                                       string path1, string path2, string cryptPassword)
        {
            MemoryStream memoryStream = null;

            try
            {
                try
                {
                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);
                }
                catch (InvalidOperationException)
                {
                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);
                }
            }
            catch (Exception ex)
            {
                Locker.ClearFileStream(ref fileStream);

                throw ex;
            }

            Queue <string> existingRawLogs = new Queue <string>();

            // if memory stream is not null (i.e. log files already exist)
            if (memoryStream != null)
            {
                StreamReader sr = new StreamReader(memoryStream);

                while (sr.Peek() >= 0)
                {
                    try
                    {
                        existingRawLogs.Enqueue(sr.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        Locker.ClearFileStream(ref fileStream);

                        sr.Close();
                        sr.Dispose();
                        memoryStream.Close();
                        memoryStream.Dispose();

                        throw ex;
                    }
                }

                sr.Close();
                sr.Dispose();
                memoryStream.Close();
                memoryStream.Dispose();
            }

            return(existingRawLogs);
        }
Esempio n. 6
0
        private static string ReadExistingLogsString(ref FileStream fileStream,
                                                     string path1, string path2, string cryptPassword)
        {
            MemoryStream memoryStream = null;

            try
            {
                try
                {
                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);
                }
                catch (InvalidOperationException)
                {
                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);
                }
            }
            catch (Exception ex)
            {
                Locker.ClearFileStream(ref fileStream);

                throw ex;
            }

            string decryptedLogs = "";

            // if memory stream is not null (i.e. log files already exist)
            if (memoryStream != null)
            {
                StreamReader sr = new StreamReader(memoryStream);

                try
                {
                    decryptedLogs = sr.ReadToEnd();
                }
                catch (Exception ex)
                {
                    Locker.ClearFileStream(ref fileStream);

                    sr.Close();
                    sr.Dispose();
                    memoryStream.Close();
                    memoryStream.Dispose();

                    throw ex;
                }

                sr.Close();
                sr.Dispose();
                memoryStream.Close();
                memoryStream.Dispose();
            }

            return(decryptedLogs);
        }
Esempio n. 7
0
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            FileStream fileStream = null;

            string existingLogs = ReadExistingLogs(ref fileStream, @"C:\OxigenData\SettingsData\ss_ad_c_1.dat",
                                                   @"C:\OxigenData\SettingsData\ss_ad_c_2.dat", "password");

            Locker.ClearFileStream(ref fileStream);

            MessageBox.Show(existingLogs);
        }
Esempio n. 8
0
        // Uploads advert specific aggregated files to relay server. If an upload fails, the lock on the read file is released
        private void UploadAdvertSpecificAggregatedFiles()
        {
            _bAdvertChannelClickProportionsUploaded = TryLockAndUploadStringFromFile(ref _advertChannelClickProportionsStream, _advertChannelClickProportionsPath, "", -1L);

            if (!_bAdvertChannelClickProportionsUploaded)
            {
                Locker.ClearFileStream(ref _advertChannelClickProportionsStream);
            }

            _bAdvertChannelImpressionProportionsUploaded = TryLockAndUploadStringFromFile(ref _advertChannelImpressionProportionsStream, _advertChannelImpressionProportionsPath, "", -1L);

            if (!_bAdvertChannelImpressionProportionsUploaded)
            {
                Locker.ClearFileStream(ref _advertChannelImpressionProportionsStream);
            }
        }
Esempio n. 9
0
        // Uploads non advert specific aggregated files to relay server. If an upload fails, the lock on the read file is released
        private void UploadNonAdvertSpecificAggregatedFiles(long channelID)
        {
            _bDateClicksPerAssetUploaded = TryLockAndUploadStringFromFile(ref _dateClicksPerAssetStream, _dateClicksPerAssetPath, "", channelID);

            if (!_bDateClicksPerAssetUploaded)
            {
                Locker.ClearFileStream(ref _dateClicksPerAssetStream);
            }

            _bDateImpressionsPerAssetUploaded = TryLockAndUploadStringFromFile(ref _dateImpressionsPerAssetStream, _dateImpressionsPerAssetPath, "", channelID);

            if (!_bDateImpressionsPerAssetUploaded)
            {
                Locker.ClearFileStream(ref _dateImpressionsPerAssetStream);
            }
        }
Esempio n. 10
0
        private void TryUploadTruncateReleaseUsageCountFile(bool bRawLogsExist)
        {
            if (!bRawLogsExist)
            {
                return;
            }

            bool bUsageCountUploaded = TryLockAndUploadStringFromFile(ref _usageCountStream, _usageCountLogsPath1, _usageCountLogsPath2, -2L);

            if (bUsageCountUploaded)
            {
                TryTruncate(_usageCountStream);
            }

            Locker.ClearFileStream(ref _usageCountStream);
        }
Esempio n. 11
0
        /// <summary>
        /// Decrypts (if encrypted) and deserializes an object from XML. Unlocks the file after operation.
        /// Operation is expected to succeed, i.e. use this call for important serialized XML objects i.e. non-log files
        /// </summary>
        /// <param name="type">expected type of the deserialized object</param>
        /// <param name="path">The path to load the xml file from</param>
        /// <param name="password">password to decrypt the file</param>
        /// <returns>the object to deserialize</returns>
        /// <exception cref="SerializationException">thrown when an error occurs during the object's deserializaton</exception>
        /// <exception cref="FileNotFoundException">thrown if XML serialized file not found</exception>
        /// <exception cref="InvalidOperationException">thrown by XmlSerializer.Deserialize</exception>
        /// <exception cref="IOException">thrown when there is an error during read</exception>
        /// <exception cref="DirectoryNotFoundException">thrown when the directory is not found</exception>
        /// <exception cref="CryptographicException">thrown when encrypted file is corrupted</exception>
        public static object Deserialize(Type type, string path, string password)
        {
            FileStream fileStream = null;

            object obj = null;

            try
            {
                obj = Deserialize(type, path, password, ref fileStream, true, false);
            }
            finally
            {
                Locker.ClearFileStream(ref fileStream);
            }

            return(obj);
        }
Esempio n. 12
0
        private void Save()
        {
            FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\EncryptedText.dat", FileMode.Create);

            try
            {
                Locker.WriteEncryptString(ref fs, textBox1.Text, "password");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            finally
            {
                Locker.ClearFileStream(ref fs);
            }

            MessageBox.Show("Encryption done. New file is on desktop.");
        }
Esempio n. 13
0
        private void DecryptShow(string fileName)
        {
            FileStream fs           = null;
            string     existingLogs = "";

            try
            {
                existingLogs = ReadExistingLogsString(ref fs, fileName, fileName, "password");
            }
            catch (Exception eX)
            {
                textBox1.Text = eX.ToString();
                return;
            }
            finally
            {
                Locker.ClearFileStream(ref fs);
            }

            textBox1.Text = existingLogs;
        }
Esempio n. 14
0
        // this method locks the deserialized file and is the overload that should be used
        // in release builds
        private static object Deserialize(Type type, ref FileStream fileStream,
                                          XmlSerializer xmlSerializer, string path, string password, bool bCritical, bool bKeepLock)
        {
            object obj = null;

            MemoryStream decryptedDataStream = Locker.ReadDecryptFile(ref fileStream, path, password, bCritical, bKeepLock);

            // if it is likely that object may not exist (i.e. non-important serialized object), do not throw an error but create a blank instance
            if (decryptedDataStream == null && !bCritical)
            {
                obj = Activator.CreateInstance(type);
                return(obj);
            }

            TextReader reader = new StreamReader(decryptedDataStream, Encoding.ASCII);

            try
            {
                obj = xmlSerializer.Deserialize(reader);
            }
            catch (Exception ex) // this can happen if deserialized object obj is not of Type type
            {
                Locker.ClearFileStream(ref fileStream);
                throw new InvalidOperationException("Type mismatch between parameter type " + type.ToString() + " and type as described in XML serialized object", ex);
            }
            finally
            {
                reader.Close();
                reader.Dispose();

                decryptedDataStream.Close();
                decryptedDataStream.Dispose();
            }

            return(obj);
        }
Esempio n. 15
0
        /// <summary>
        /// Opens and locks a log file and returns a string with the decrypted logs if they exist.
        /// If they dont exist, an empty string is returned.
        /// </summary>
        /// <param name="fileStream">FileStream to read and lock a log file</param>
        /// <param name="path1">Path of first file to try</param>
        /// <param name="path2">Path of second file to try if first file is locked</param>
        /// <param name="cryptPassword">password to use for encryption/decryption</param>
        /// <returns>a string with the decrypted logs or an empty string if logs don't exist</returns>
        /// <param name="logger">the Logger object to use when debugging exceptions</param>
        private static Queue <string> ReadExistingLogs(ref FileStream fileStream,
                                                       string path1, string path2, string cryptPassword, Logger logger)
        {
            MemoryStream memoryStream = null;

            try
            {
                try
                {
                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);

                    logger.WriteMessage(DateTime.Now.ToString() + " successfully read existing log from path 1.");
                }
                catch (IOException ex)
                {
                    logger.WriteError(ex);

                    memoryStream = Locker.ReadDecryptFile(ref fileStream, path1, cryptPassword, false, true);

                    logger.WriteMessage(DateTime.Now.ToString() + " successfully read existing log from path 2.");
                }
            }
            catch (CryptographicException ex) // file will be truncate in the case of a CryptographicException
            {
                logger.WriteError(ex);
            }
            catch (Exception ex)
            {
                Locker.ClearFileStream(ref fileStream);

                logger.WriteMessage(DateTime.Now.ToString() + " successfully unlocked file. Please see below for error.");

                throw ex;
            }

            Queue <string> existingRawLogs = new Queue <string>();

            // if memory stream is not null (i.e. log files already exist)
            if (memoryStream != null)
            {
                StreamReader sr = new StreamReader(memoryStream);

                logger.WriteMessage(DateTime.Now.ToString() + " successfully created stream reader.");

                while (sr.Peek() >= 0)
                {
                    try
                    {
                        existingRawLogs.Enqueue(sr.ReadLine());
                    }
                    catch (Exception ex)
                    {
                        Locker.ClearFileStream(ref fileStream);

                        logger.WriteMessage(DateTime.Now.ToString() + " successfully unlocked file. Please see below for error.");

                        sr.Close();
                        sr.Dispose();

                        logger.WriteMessage(DateTime.Now.ToString() + " successfully disposed of StreamReader.");

                        memoryStream.Close();
                        memoryStream.Dispose();

                        logger.WriteMessage(DateTime.Now.ToString() + " successfully disposed of MemoryStream.");

                        throw ex;
                    }
                }

                sr.Close();
                sr.Dispose();

                logger.WriteMessage(DateTime.Now.ToString() + " successfully disposed of StreamReader.");

                memoryStream.Close();
                memoryStream.Dispose();

                logger.WriteMessage(DateTime.Now.ToString() + " successfully disposed of MemoryStream.");
            }

            return(existingRawLogs);
        }
Esempio n. 16
0
        /// <summary>
        /// Serializes an object to XML and encrypts it to a file that has been locked from a previous operation.
        /// The lock is then released.
        /// </summary>
        /// <param name="obj">The object to serialise</param>
        /// <param name="password">The encryption password</param>
        /// <param name="fileStream">the FileStream that has a lock on the file to serialize to</param>
        /// <exception cref="SerializationException">thrown when an error occurs during the object's serializaton</exception>
        /// <exception cref="InvalidOperationException">thrown when there is a problem in serializaton</exception>
        /// <exception cref="IOException">thrown when there is an error writing the file to disk</exception>
        /// <exception cref="NullReferenceException">the object to serialize is null</exception>
        public static void Serialize(object obj, string password, ref FileStream fileStream)
        {
            if (obj == null)
            {
                Locker.ClearFileStream(ref fileStream);
                throw new NullReferenceException("object is null");
            }

            XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());

            MemoryStream stream = new MemoryStream();

            StreamWriter encodingWriter = new StreamWriter(stream, Encoding.ASCII);

            try
            {
                xmlSerializer.Serialize(encodingWriter, obj);
            }
            catch (InvalidOperationException ex)
            {
                stream.Close();
                stream.Dispose();

                encodingWriter.Close();
                encodingWriter.Dispose();

                Locker.ClearFileStream(ref fileStream);

                throw new InvalidOperationException("Error serializing object of type " + obj.GetType(), ex);
            }
            catch (Exception ex)
            {
                stream.Close();
                stream.Dispose();

                encodingWriter.Close();
                encodingWriter.Dispose();

                Locker.ClearFileStream(ref fileStream);

                throw ex;
            }

            stream.Position = 0;

            byte[] dataToEncrypt = new byte[stream.Length + (16 - stream.Length % 16)];
            stream.Read(dataToEncrypt, 0, dataToEncrypt.Length);

            stream.Close();
            stream.Dispose();

            encodingWriter.Close();
            encodingWriter.Dispose();

            // Encrypt
            byte[] encryptedData = Cryptography.Encrypt(dataToEncrypt, password);

            int noBytes = encryptedData.Length;

            fileStream.SetLength(0);

            try
            {
                fileStream.Write(encryptedData, 0, noBytes);
            }
            finally
            {
                Locker.ClearFileStream(ref fileStream);
            }
        }