예제 #1
0
        public void Validate(ValidationContext context, XDocument rulePackXDocument)
        {
            List <XElement> list = XmlProcessingUtils.GetFingerprintProcessorsInRulePackage(rulePackXDocument).ToList <XElement>();

            if (context != null && context.IsPayloadFingerprintsRuleCollection)
            {
                int num = (int)DataClassificationConfigSchema.MaxFingerprints.DefaultValue;
                if (context.DcValidationConfig != null)
                {
                    num = context.DcValidationConfig.MaxFingerprints;
                }
                if (list.Count > num)
                {
                    throw new ClassificationRuleCollectionFingerprintValidationException(Strings.ClassificationRuleCollectionFingerprintsExceedLimit(list.Count, num), null);
                }
            }
            Dictionary <string, HashSet <string> > dictionary = new Dictionary <string, HashSet <string> >();
            List <string> list2 = new List <string>();

            foreach (XElement xelement in list)
            {
                byte[] array = null;
                try
                {
                    array = Convert.FromBase64String(xelement.Value);
                }
                catch (FormatException)
                {
                }
                if (array == null || array.Length % 2 != 0)
                {
                    list2.Add(xelement.Attribute("id").Value);
                }
                else
                {
                    string           item    = string.Format("{0}_{1}", xelement.Attribute("threshold").Value, xelement.Attribute("shingleCount").Value);
                    HashSet <string> hashSet = null;
                    dictionary.TryGetValue(xelement.Value, out hashSet);
                    if (hashSet == null)
                    {
                        hashSet = new HashSet <string>();
                    }
                    if (!hashSet.Contains(item))
                    {
                        hashSet.Add(item);
                    }
                    else
                    {
                        list2.Add(xelement.Attribute("id").Value);
                    }
                    dictionary[xelement.Value] = hashSet;
                }
            }
            if (list2.Count > 0)
            {
                throw new ClassificationRuleCollectionFingerprintValidationException(Strings.ClassificationRuleCollectionFingerprintValidationFailure(string.Join(Strings.ClassificationRuleCollectionOffendingListSeparator, list2)), null);
            }
        }
예제 #2
0
        internal static bool OptimizeRulePackXDoc(XDocument rulePackXDoc, DataClassificationConfig dataClassificationConfig)
        {
            ArgumentValidator.ThrowIfNull("rulePackXDoc", rulePackXDoc);
            bool     result   = false;
            XElement xelement = rulePackXDoc.Descendants(XmlProcessingUtils.GetMceNsQualifiedNodeName("Rules")).FirstOrDefault <XElement>();

            if (xelement == null)
            {
                throw new ClassificationRuleCollectionInvalidObjectException(Strings.ClassificationRuleCollectionInvalidObject);
            }
            List <XElement> list = (from versionElement in xelement.Descendants(XmlProcessingUtils.GetMceNsQualifiedNodeName("Version"))
                                    where versionElement.IsEmpty
                                    select versionElement).ToList <XElement>();

            foreach (XElement xelement2 in list)
            {
                xelement2.Remove();
                result = true;
            }
            List <string> list2 = (from versionedTextProcessorReference in TextProcessorUtils.GetTextProcessorReferences(rulePackXDoc.Root)
                                   select versionedTextProcessorReference.Key).Distinct(ClassificationDefinitionConstants.TextProcessorIdComparer).ToList <string>();
            List <XElement> list3 = XmlProcessingUtils.GetFingerprintProcessorsInRulePackage(rulePackXDoc).ToList <XElement>();

            foreach (XElement xelement3 in list3)
            {
                XAttribute xattribute = xelement3.Attribute("id");
                string     item       = (xattribute == null) ? string.Empty : xattribute.Value;
                if (list2.Contains(item))
                {
                    if (dataClassificationConfig != null)
                    {
                        XAttribute xattribute2 = xelement3.Attribute("threshold");
                        if (xattribute2 != null && !dataClassificationConfig.FingerprintThreshold.ToString().Equals(xattribute2.Value, StringComparison.Ordinal))
                        {
                            xattribute2.Value = dataClassificationConfig.FingerprintThreshold.ToString();
                            result            = true;
                        }
                    }
                }
                else
                {
                    xelement3.Remove();
                    result = true;
                }
            }
            return(result);
        }