| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Xml; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel |
| | | 8 | | { |
| | | 9 | | internal struct XmlAttributeHolder |
| | | 10 | | { |
| | 0 | 11 | | public static XmlAttributeHolder[] emptyArray = Array.Empty<XmlAttributeHolder>(); |
| | | 12 | | |
| | | 13 | | public XmlAttributeHolder(string prefix, string localName, string ns, string value) |
| | | 14 | | { |
| | 0 | 15 | | Prefix = prefix; |
| | 0 | 16 | | LocalName = localName; |
| | 0 | 17 | | NamespaceUri = ns; |
| | 0 | 18 | | Value = value; |
| | 0 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | public string Prefix { get; } |
| | | 22 | | |
| | 0 | 23 | | public string NamespaceUri { get; } |
| | | 24 | | |
| | 0 | 25 | | public string LocalName { get; } |
| | | 26 | | |
| | 0 | 27 | | public string Value { get; } |
| | | 28 | | |
| | | 29 | | public void WriteTo(XmlWriter writer) |
| | | 30 | | { |
| | 0 | 31 | | writer.WriteStartAttribute(Prefix, LocalName, NamespaceUri); |
| | 0 | 32 | | writer.WriteString(Value); |
| | 0 | 33 | | writer.WriteEndAttribute(); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static void WriteAttributes(XmlAttributeHolder[] attributes, XmlWriter writer) |
| | | 37 | | { |
| | 0 | 38 | | for (int i = 0; i < attributes.Length; i++) |
| | | 39 | | { |
| | 0 | 40 | | attributes[i].WriteTo(writer); |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public static XmlAttributeHolder[] ReadAttributes(XmlDictionaryReader reader) |
| | | 45 | | { |
| | 0 | 46 | | int maxSizeOfHeaders = int.MaxValue; |
| | 0 | 47 | | return ReadAttributes(reader, ref maxSizeOfHeaders); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public static XmlAttributeHolder[] ReadAttributes(XmlDictionaryReader reader, ref int maxSizeOfHeaders) |
| | | 51 | | { |
| | 0 | 52 | | if (reader.AttributeCount == 0) |
| | | 53 | | { |
| | 0 | 54 | | return emptyArray; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | XmlAttributeHolder[] attributes = new XmlAttributeHolder[reader.AttributeCount]; |
| | 0 | 58 | | reader.MoveToFirstAttribute(); |
| | 0 | 59 | | for (int i = 0; i < attributes.Length; i++) |
| | | 60 | | { |
| | 0 | 61 | | string ns = reader.NamespaceURI; |
| | 0 | 62 | | string localName = reader.LocalName; |
| | 0 | 63 | | string prefix = reader.Prefix; |
| | 0 | 64 | | string value = string.Empty; |
| | 0 | 65 | | while (reader.ReadAttributeValue()) |
| | | 66 | | { |
| | 0 | 67 | | if (value.Length == 0) |
| | | 68 | | { |
| | 0 | 69 | | value = reader.Value; |
| | | 70 | | } |
| | | 71 | | else |
| | | 72 | | { |
| | 0 | 73 | | value += reader.Value; |
| | | 74 | | } |
| | | 75 | | } |
| | 0 | 76 | | Deduct(prefix, ref maxSizeOfHeaders); |
| | 0 | 77 | | Deduct(localName, ref maxSizeOfHeaders); |
| | 0 | 78 | | Deduct(ns, ref maxSizeOfHeaders); |
| | 0 | 79 | | Deduct(value, ref maxSizeOfHeaders); |
| | 0 | 80 | | attributes[i] = new XmlAttributeHolder(prefix, localName, ns, value); |
| | 0 | 81 | | reader.MoveToNextAttribute(); |
| | | 82 | | } |
| | 0 | 83 | | reader.MoveToElement(); |
| | 0 | 84 | | return attributes; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | private static void Deduct(string s, ref int maxSizeOfHeaders) |
| | | 88 | | { |
| | 0 | 89 | | int byteCount = s.Length * sizeof(char); |
| | 0 | 90 | | if (byteCount > maxSizeOfHeaders) |
| | | 91 | | { |
| | 0 | 92 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SRCommon.XmlBuff |
| | | 93 | | } |
| | 0 | 94 | | maxSizeOfHeaders -= byteCount; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public static string GetAttribute(XmlAttributeHolder[] attributes, string localName, string ns) |
| | | 98 | | { |
| | 0 | 99 | | for (int i = 0; i < attributes.Length; i++) |
| | | 100 | | { |
| | 0 | 101 | | if (attributes[i].LocalName == localName && attributes[i].NamespaceUri == ns) |
| | | 102 | | { |
| | 0 | 103 | | return attributes[i].Value; |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | return null; |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |