public virtual Differences VisitSecurityAttributeList(SecurityAttributeList list1, SecurityAttributeList list2,
   out SecurityAttributeList changes, out SecurityAttributeList deletions, out SecurityAttributeList insertions){
   changes = list1 == null ? null : list1.Clone();
   deletions = list1 == null ? null : list1.Clone();
   insertions = list1 == null ? new SecurityAttributeList() : list1.Clone();
   //^ assert insertions != null;
   Differences differences = new Differences();
   for (int j = 0, n = list2 == null ? 0 : list2.Count; j < n; j++){
     //^ assert list2 != null;
     SecurityAttribute nd2 = list2[j];
     if (nd2 == null) continue;
     insertions.Add(null);
   }
   TrivialHashtable savedDifferencesMapFor = this.differencesMapFor;
   this.differencesMapFor = null;
   TrivialHashtable matchedNodes = new TrivialHashtable();
   for (int i = 0, k = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
     //^ assert list1 != null && changes != null && deletions != null;
     SecurityAttribute nd1 = list1[i]; 
     if (nd1 == null) continue;
     Differences diff;
     int j;
     SecurityAttribute nd2 = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out diff, out j);
     if (nd2 == null || diff == null){Debug.Assert(nd2 == null && diff == null); continue;}
     matchedNodes[nd1.UniqueKey] = nd1;
     matchedNodes[nd2.UniqueKey] = nd2;
     changes[i] = diff.Changes as SecurityAttribute;
     deletions[i] = diff.Deletions as SecurityAttribute;
     insertions[i] = diff.Insertions as SecurityAttribute;
     insertions[n+j] = nd1; //Records the position of nd2 in list2 in case the change involved a permutation
     Debug.Assert(diff.Changes == changes[i] && diff.Deletions == deletions[i] && diff.Insertions == insertions[i]);
     differences.NumberOfDifferences += diff.NumberOfDifferences;
     differences.NumberOfSimilarities += diff.NumberOfSimilarities;
   }
   //Find deletions
   for (int i = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
     //^ assert list1 != null;
     SecurityAttribute nd1 = list1[i]; 
     if (nd1 == null) continue;
     if (matchedNodes[nd1.UniqueKey] != null) continue;
     if (changes != null) changes[i] = null;
     if (deletions != null) deletions[i] = nd1;
     insertions[i] = null;
     differences.NumberOfDifferences += 1;
   }
   //Find insertions
   for (int j = 0, n = list1 == null ? 0 : list1.Count, m = list2 == null ? 0 : list2.Count; j < m; j++){
     //^ assert list2 != null;
     SecurityAttribute nd2 = list2[j]; 
     if (nd2 == null) continue;
     if (matchedNodes[nd2.UniqueKey] != null) continue;
     insertions[n+j] = nd2;  //Records nd2 as an insertion into list1, along with its position in list2
     differences.NumberOfDifferences += 1; //REVIEW: put the size of the tree here?
   }
   if (differences.NumberOfDifferences == 0){
     changes = null;
     deletions = null;
     insertions = null;
   }
   this.differencesMapFor = savedDifferencesMapFor;
   return differences;
 }
Beispiel #2
0
 public virtual SecurityAttributeList VisitSecurityAttributeList(SecurityAttributeList attributes, SecurityAttributeList changes, SecurityAttributeList deletions, SecurityAttributeList insertions){
   if (attributes == null) return changes;
   if (changes != null){
     if (deletions == null || insertions == null)
       Debug.Assert(false);
     else{
     }
   }else if (deletions != null)
     return null;
   return attributes;
 }
 public virtual SecurityAttributeList VisitSecurityAttributeList(SecurityAttributeList attributes)
 {
     if (attributes == null) return null;
     for (int i = 0, n = attributes.Count; i < n; i++)
         attributes[i] = this.VisitSecurityAttribute(attributes[i]);
     return attributes;
 }
 public virtual SecurityAttribute GetClosestMatch(SecurityAttribute/*!*/ nd1, SecurityAttributeList/*!*/ list1, SecurityAttributeList list2, int list1pos, ref int list2start,
   TrivialHashtable/*!*/ matchedNodes, out Differences closestDifferences, out int list2pos) {
   closestDifferences = null; list2pos = -1;
   if (list2 == null) return null;
   if (nd1 == null || list1 == null || matchedNodes == null || list1pos < 0 || list1pos >= list1.Count || list2start < 0 || list2start >= list2.Count) {
     Debug.Assert(false); return null;
   }
   SecurityAttribute closest = null;
   Differences winnerSoFar = null;
   for (int j = list2start, m = list2.Count; j < m; j++){
     SecurityAttribute nd2 = list2[j];
     if (list2start == j) list2start++;
     if (nd2 == null) continue;
     if (matchedNodes[nd2.UniqueKey] != null) continue;
     Differences diff = this.GetDifferences(nd1, nd2);
     if (diff == null){Debug.Assert(false); continue;}
     if (diff.Similarity <= 0.5){
       //Not a good enough match
       if (list2start == j+1) list2start--; //The next call to GetClosestMatch will start looking at list2start, so this node will be considered then
       continue; //ignore it for the rest of this call
     }
     if (winnerSoFar != null && winnerSoFar.Similarity >= diff.Similarity) continue;
     winnerSoFar = closestDifferences = diff;
     closest = nd2;
     list2pos = j;
     if (diff.NumberOfDifferences == 0) return closest; //Perfect match, no need to look for other matches
   }
   if (closest != null){
     //^ assert winnerSoFar != null;
     //closest is closer to nd1 than any other node in list2, but this is no good if some other node in list1 has a better claim on closest
     for (int i = list1pos+1, n = list1.Count; i < n; i++){
       SecurityAttribute nd1alt = list1[i];
       if (nd1alt == null) continue;
       if (matchedNodes[nd1alt.UniqueKey] != null) continue;
       Differences diff = this.GetDifferences(nd1alt, closest);
       if (diff == null){Debug.Assert(false); continue;}
       if (diff.Similarity <= winnerSoFar.Similarity) continue;
       //nd1alt has a better claim on closest. See if it wants closest.
       Differences diff2;
       int j, k = list2start;
       SecurityAttribute nd2alt = this.GetClosestMatch(nd1alt, list1, list2, i, ref k,  matchedNodes, out diff2, out j);
       if (nd2alt != closest){
         Debug.Assert(nd2alt != null && diff2 != null && diff2.Similarity >= diff.Similarity);
         continue; //nd1alt prefers nd2alt to closest, so closest is still available
       }
       //nd1alt wants closest, take it out of the running
       matchedNodes[closest.UniqueKey] = nd1alt;
       //Now that closest is out of the running, try again
       k = list2start;
       SecurityAttribute newClosest = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out winnerSoFar, out list2pos);
       //put closest back in the running so that the next call to this routine will pick it up
       matchedNodes[closest.UniqueKey] = closest;
       closest = newClosest;
       break;
     }
   }
   closestDifferences = winnerSoFar;
   return closest;
 }
Beispiel #5
0
 private AttributeNode[] GetExposedAttributes(AttributeList attributes, SecurityAttributeList securityAttributes) {
     if (attributes == null) Console.WriteLine("null attribute list");
     if (securityAttributes == null) Console.WriteLine("null security attribute list");
     List < AttributeNode > exposedAttributes = new List < AttributeNode >();
     for (int i = 0; i < attributes.Count; i++) {
         AttributeNode attribute = attributes[i];
         if (attribute == null) Console.WriteLine("null attribute");
         if (this.ApiFilter.IsExposedAttribute(attribute)) exposedAttributes.Add(attribute);
     }
     for (int i = 0; i < securityAttributes.Count; i++) {
         SecurityAttribute securityAttribute = securityAttributes[i];
         if (securityAttribute == null) Console.WriteLine("null security attribute");
         AttributeList permissionAttributes = securityAttribute.PermissionAttributes;
         //if (permissionAttributes == null) Console.WriteLine("null permission attribute list");
         if (permissionAttributes == null) continue;
         for (int j = 0; j < permissionAttributes.Count; j++) {
             AttributeNode permissionAttribute = permissionAttributes[j];
             //if (permissionAttribute == null) Console.WriteLine("null permission attribute");
             // saw an example where this was null; ildasm shows no permission attribute, so skip it
             if (permissionAttribute == null) continue;
             if (this.ApiFilter.IsExposedAttribute(permissionAttribute)) exposedAttributes.Add(permissionAttribute);
         }
     }
     return (exposedAttributes.ToArray());
 }
Beispiel #6
0
        private void WriteMember(Member member, TypeNode type) {
            //writer.WriteStartElement("api");
            //writer.WriteAttributeString("id", namer.GetMemberName(member));
            //Console.WriteLine("member: {0}", namer.GetMemberName(member));

            WriteApiData(member);
            WriteMemberData(member);

            SecurityAttributeList securityAttributes = new SecurityAttributeList();
            switch (member.NodeType) {
                case NodeType.Field:
                    Field field = (Field)member;
                    WriteFieldData(field);
                    WriteValue(field.Type);
                    // write enumeration field values; expand later to all literal field values?
                    if (field.DeclaringType.NodeType == NodeType.EnumNode) {
                        WriteLiteral(new Literal(field.DefaultValue.Value, ((EnumNode)field.DeclaringType).UnderlyingType), false);
                    }
                    break;
                case NodeType.Method:
                    Method method = (Method)member;
                    WriteMethodData(method);

                    // write the templates node with either the generic template params or the specialized template arguments
                    if (method.TemplateArguments != null)
                        WriteSpecializedTemplateArguments(method.TemplateArguments);
                    else
                        WriteGenericParameters(method.TemplateParameters);

                    WriteParameters(method.Parameters);
                    WriteValue(method.ReturnType);
                    WriteImplementedMembers(ReflectionUtilities.GetImplementedMethods(method));

                    if (method.SecurityAttributes != null) securityAttributes = method.SecurityAttributes;
                    break;
                case NodeType.Property:
                    Property property = (Property)member;
                    WritePropertyData(property);
                    WriteParameters(property.Parameters);
                    WriteValue(property.Type);
                    WriteImplementedMembers(ReflectionUtilities.GetImplementedProperties(property));
                    break;
                case NodeType.Event:
                    Event trigger = (Event)member;
                    WriteEventData(trigger);
                    WriteImplementedMembers(ReflectionUtilities.GetImplementedEvents(trigger));
                    break;
                case NodeType.InstanceInitializer:
                case NodeType.StaticInitializer:
                    Method constructor = (Method)member;
                    WriteParameters(constructor.Parameters);
                    break;

            }

            WriteMemberContainers(member, type);

            WriteAttributes(member.Attributes, securityAttributes);

            //writer.WriteEndElement();
        }
 public virtual SecurityAttributeList VisitSecurityAttributeList(SecurityAttributeList attributes1, SecurityAttributeList attributes2)
 {
     if (attributes1 == null) return null;
     for (int i = 0, n = attributes1.Count, m = attributes2 == null ? 0 : attributes2.Count; i < n; i++)
     {
         //^ assert attributes2 != null;
         if (i >= m)
             attributes1[i] = this.VisitSecurityAttribute(attributes1[i], null);
         else
             attributes1[i] = this.VisitSecurityAttribute(attributes1[i], attributes2[i]);
     }
     return attributes1;
 }
Beispiel #8
0
        // Attributes

        protected void WriteAttributes(AttributeList attributes, SecurityAttributeList securityAttributes) {
            AttributeNode[] exposed = GetExposedAttributes(attributes, securityAttributes);
            if (exposed.Length == 0) return;
            writer.WriteStartElement("attributes");
            for (int i = 0; i < exposed.Length; i++) {
                AttributeNode attribute = exposed[i];
                writer.WriteStartElement("attribute");

                TypeNode type = attribute.Type;
                WriteTypeReference(attribute.Type);
                // WriteStringAttribute("type", namer.GetApiName(attribute.Type));

                ExpressionList expressions = attribute.Expressions;
                for (int j = 0; j < expressions.Count; j++) {
                    WriteExpression(expressions[j]);
                }

                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
Beispiel #9
0
 public void ExtractSecurityAttributes(AttributeList attributes, ref SecurityAttributeList securityAttributes){
   if (attributes == null){Debug.Assert(false); return;}
   TrivialHashtable attributeListFor = null;
   for (int i = 0, n = attributes.Count; i < n; i++){
     AttributeNode attr = attributes[i];
     if (attr == null) continue;
     if (attr.Type == null) continue;
     if (!this.GetTypeView(attr.Type).IsAssignableTo(SystemTypes.SecurityAttribute)) continue;
     attributes[i] = null;
     ExpressionList args = attr.Expressions;
     if (args == null || args.Count < 1) return;
     Literal lit = args[0] as Literal;
     if (lit == null || !(lit.Value is int)) return;
     int action = (int)lit.Value;
     if (attributeListFor == null) attributeListFor = new TrivialHashtable();
     AttributeList attrsForAction = (AttributeList)attributeListFor[action+1];
     if (attrsForAction == null){
       attributeListFor[action+1] = attrsForAction = new AttributeList();
       SecurityAttribute secAttr = new SecurityAttribute();
       secAttr.Action = (System.Security.Permissions.SecurityAction)action;
       secAttr.PermissionAttributes = attrsForAction;
       if (securityAttributes == null) securityAttributes = new SecurityAttributeList();
       securityAttributes.Add(secAttr);
     }
     attrsForAction.Add(attr);
   }
 }
Beispiel #10
0
        public override SecurityAttributeList VisitSecurityAttributeList(SecurityAttributeList attributes)
        {
            if(attributes == null)
                return null;

            return base.VisitSecurityAttributeList(new SecurityAttributeList(attributes));
        }
Beispiel #11
0
        private SecurityAttributeList GetSecurityAttributesFor(int parentIndex)
        {
            DeclSecurityRow[] securityAttributes = this.tables.DeclSecurityTable;
            SecurityAttributeList attributes = new SecurityAttributeList();
            try
            {
                int i = 0, n = securityAttributes.Length, j = n - 1;
                if (n == 0) return attributes;
                bool sorted = (this.sortedTablesMask >> (int)TableIndices.DeclSecurity) % 2 == 1;
                if (sorted)
                {
                    while (i < j)
                    {
                        int k = (i + j) / 2;
                        if (securityAttributes[k].Parent < parentIndex)
                            i = k + 1;
                        else
                            j = k;
                    }
                    while (i > 0 && securityAttributes[i - 1].Parent == parentIndex) i--;
                }
                for (; i < n; i++)
                    if (securityAttributes[i].Parent == parentIndex)
                        attributes.Add(this.GetSecurityAttribute(i));
                    else if (sorted)
                        break;
            }
            catch (Exception e)
            {
                if (this.module == null) return attributes;
                if (this.module.MetadataImportErrors == null) this.module.MetadataImportErrors = new ArrayList();
                this.module.MetadataImportErrors.Add(e);
            }

            return attributes;
        }
Beispiel #12
0
        /// <summary>
        /// Write out information for a member
        /// </summary>
        /// <param name="member">The member to write out</param>
        /// <param name="type">The declaring type of the member</param>
        private void WriteMember(Member member, TypeNode type)
        {
            this.WriteApiData(member);
            this.WriteMemberData(member);

            SecurityAttributeList securityAttributes = new SecurityAttributeList();

            switch(member.NodeType)
            {
                case NodeType.Field:
                    Field field = (Field)member;

                    this.WriteFieldData(field);
                    this.WriteReturnValue(field.Type);

                    // Write enumeration and literal (constant) field values
                    if(field.DeclaringType.NodeType == NodeType.EnumNode)
                    {
                        this.WriteLiteral(new Literal(field.DefaultValue.Value,
                            ((EnumNode)field.DeclaringType).UnderlyingType), false);
                    }
                    else
                        if(field.IsLiteral)
                            this.WriteLiteral(new Literal(field.DefaultValue.Value, field.Type), false);
                    break;

                case NodeType.Method:
                    Method method = (Method)member;

                    this.WriteProcedureData(method, method.OverriddenMember);

                    // Write the templates node with either the generic template parameters or the specialized
                    // template arguments.
                    if(method.TemplateArguments != null)
                        this.WriteSpecializedTemplateArguments(method.TemplateArguments);
                    else
                        this.WriteGenericParameters(method.TemplateParameters);

                    this.WriteParameters(method.Parameters);
                    this.WriteReturnValue(method.ReturnType);
                    this.WriteImplementedMembers(method.GetImplementedMethods());

                    if(method.SecurityAttributes != null)
                        securityAttributes = method.SecurityAttributes;
                    break;

                case NodeType.Property:
                    Property property = (Property)member;

                    this.WritePropertyData(property);
                    this.WriteParameters(property.Parameters);
                    this.WriteReturnValue(property.Type);
                    this.WriteImplementedMembers(property.GetImplementedProperties());
                    break;

                case NodeType.Event:
                    Event trigger = (Event)member;

                    this.WriteEventData(trigger);
                    this.WriteImplementedMembers(trigger.GetImplementedEvents());
                    break;

                case NodeType.InstanceInitializer:
                case NodeType.StaticInitializer:
                    Method constructor = (Method)member;

                    this.WriteParameters(constructor.Parameters);
                    break;
            }

            this.WriteMemberContainers(type);
            this.WriteAttributes(member.Attributes, securityAttributes);
        }
Beispiel #13
0
        /// <summary>
        /// This is used to get an enumerable list of the exposed attributes on a member
        /// </summary>
        /// <param name="attributes">The general attributes</param>
        /// <param name="securityAttributes">The security attributes</param>
        /// <returns>An enumerable list of the exposed attributes on the member</returns>
        private IEnumerable<AttributeNode> GetExposedAttributes(AttributeList attributes,
          SecurityAttributeList securityAttributes)
        {
            if(attributes == null)
                throw new ArgumentNullException("attributes");

            if(securityAttributes == null)
                throw new ArgumentNullException("securityAttributes");

            foreach(var attribute in attributes)
            {
                if(attribute == null)
                    throw new InvalidOperationException("Null attribute found");

                if(this.ApiFilter.IsExposedAttribute(attribute))
                    yield return attribute;
            }

            foreach(var securityAttribute in securityAttributes)
            {
                if(securityAttribute == null)
                    throw new InvalidOperationException("Null security attribute found");

                AttributeList permissionAttributes = securityAttribute.PermissionAttributes;
                
                if(permissionAttributes == null)
                    continue;

                foreach(var permissionAttribute in permissionAttributes)
                {
                    // Saw an example where this was null; ildasm shows no permission attribute, so skip it
                    if(permissionAttribute == null)
                        continue;

                    if(this.ApiFilter.IsExposedAttribute(permissionAttribute))
                        yield return permissionAttribute;
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Write out attributes
        /// </summary>
        /// <param name="attributes">The standard attributes to write</param>
        /// <param name="securityAttributes">The security attributes to write</param>
        protected void WriteAttributes(AttributeList attributes, SecurityAttributeList securityAttributes)
        {
            var exposed = this.GetExposedAttributes(attributes, securityAttributes);

            if(exposed.Count() != 0)
            {
                writer.WriteStartElement("attributes");

                foreach(var attribute in exposed)
                {
                    writer.WriteStartElement("attribute");

                    this.WriteTypeReference(attribute.Type);

                    foreach(var expression in attribute.Expressions)
                        this.WriteExpression(expression);

                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
            }
        }
 public EventingVisitor(Action<SecurityAttributeList> visitSecurityAttributeList) { VisitedSecurityAttributeList += visitSecurityAttributeList; } public event Action<SecurityAttributeList> VisitedSecurityAttributeList; public override SecurityAttributeList VisitSecurityAttributeList(SecurityAttributeList attributes) { if (VisitedSecurityAttributeList != null) VisitedSecurityAttributeList(attributes); return base.VisitSecurityAttributeList(attributes); }