示例#1
0
        /// <summary>
        /// Decompresses a zipped <see cref="byte"/> array.
        /// </summary>
        /// <param name="data"></param>
        /// <returns>item1: List of unsecurebytes, item2: Tuple with Guids and bytes as Stacks</returns>
        public static Tuple <Stack <byte[]>, Tuple <Stack <Guid>, Stack <byte[]> > > GetEntriesFromZipArchive(byte[] data, bool protectedMemory = false)
        {
            using (var zippedStream = new MemoryStream(data))
            {
                using (var archive = new ZipArchive(zippedStream))
                {
                    // unsecure data
                    var openData = new Stack <byte[]>();
                    // sensible data
                    var guids         = new Stack <Guid>();
                    var sensibleBytes = new Stack <byte[]>();
                    var sensibleData  = Tuple.Create(guids, sensibleBytes);
                    var entries       = Tuple.Create(openData, sensibleData);
                    foreach (var entry in archive.Entries)
                    {
                        bool sensible = false;

                        if (entry.FullName.StartsWith($"{TermHelper.GetDatabaseTerm()}/ByteModel"))
                        {
                            sensible = true;
                        }

                        if (entry != null)
                        {
                            using (var unzippedEntryStream = entry.Open())
                            {
                                using (var ms = new MemoryStream())
                                {
                                    unzippedEntryStream.CopyTo(ms);
                                    var unzippedArray = ms.ToArray();
                                    if (sensible)
                                    {
                                        entries.Item2.Item1.Push(new Guid(entry.Name));
                                        entries.Item2.Item2.Push(unzippedArray);
                                    }
                                    else
                                    {
                                        entries.Item1.Push(unzippedArray);
                                    }
                                }
                            }
                        }
                    }
                    return(entries);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Gets the value of the given setting name. Returns defautl(T) if setting has no value right now => Void.
        /// Throws <see cref="FileLoadException"/> if a setting doesnt exist.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T GetSettingValue <T>(string name)
        {
            if (!_wellKnownSettings.ContainsKey(name))
            {
                throw new FileLoadException($"The given setting does not exist: {name}");
            }

            var setDoc         = XDocument.Load(IOPathHelper.GetSettingsFile());
            var setDocElements = setDoc.Root.Elements();

            foreach (var element in setDocElements)
            {
                if (element.Name == name)
                {
                    if (element.Value == "Void")
                    {
                        return(default(T));
                    }

                    if (ValidateSetting(name, element.Value))
                    {
                        // Cast the value to the right type that is being given by the settingType
                        var normalString = element.Value.ToString();
                        return((T)Convert.ChangeType(normalString, typeof(T)));
                    }
                    else
                    {
                        Communication.InformUser
                            ("An irregularity was detected in your configuration file. " +
                            "If you are not aware of ever having changed that file directly, the Guardian suggests starting a scan within the fortress and your Antivirus-Software, " +
                            $"for it is possible that something malicious crawled through your files. The {TermHelper.GetDatabaseTerm()} however remains save.");
                    }
                }
            }
            return(default(T));
        }