| | | 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.IO; |
| | | 6 | | using System.Security.Cryptography; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel |
| | | 10 | | { |
| | | 11 | | internal sealed class CanonicalizationDriver |
| | | 12 | | { |
| | | 13 | | private XmlReader _reader; |
| | | 14 | | private string[] _inclusivePrefixes; |
| | | 15 | | |
| | 0 | 16 | | public bool CloseReadersAfterProcessing { get; set; } |
| | | 17 | | |
| | 0 | 18 | | public bool IncludeComments { get; set; } |
| | | 19 | | |
| | | 20 | | public string[] GetInclusivePrefixes() |
| | | 21 | | { |
| | 0 | 22 | | return _inclusivePrefixes; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public void Reset() |
| | | 26 | | { |
| | 0 | 27 | | _reader = (XmlReader)null; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public void SetInclusivePrefixes(string[] inclusivePrefixes) |
| | | 31 | | { |
| | 0 | 32 | | _inclusivePrefixes = inclusivePrefixes; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public void SetInput(Stream stream) |
| | | 36 | | { |
| | 0 | 37 | | if (stream == null) |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream)); |
| | 0 | 39 | | _reader = XmlReader.Create(stream); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public void SetInput(XmlReader reader) |
| | | 43 | | { |
| | 0 | 44 | | _reader = reader ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public byte[] GetBytes() |
| | | 48 | | { |
| | 0 | 49 | | return GetMemoryStream().ToArray(); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public MemoryStream GetMemoryStream() |
| | | 53 | | { |
| | 0 | 54 | | MemoryStream memoryStream = new MemoryStream(); |
| | 0 | 55 | | WriteTo((Stream)memoryStream); |
| | 0 | 56 | | memoryStream.Seek(0L, SeekOrigin.Begin); |
| | 0 | 57 | | return memoryStream; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public void WriteTo(HashAlgorithm hashAlgorithm) |
| | | 61 | | { |
| | 0 | 62 | | WriteTo((Stream)new HashStream(hashAlgorithm)); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public void WriteTo(Stream canonicalStream) |
| | | 66 | | { |
| | 0 | 67 | | if (_reader == null) |
| | 0 | 68 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((Exception)new InvalidOperationException(SR.Fo |
| | 0 | 69 | | if (_reader is XmlDictionaryReader reader && reader.CanCanonicalize) |
| | | 70 | | { |
| | 0 | 71 | | _ = (int)reader.MoveToContent(); |
| | 0 | 72 | | reader.StartCanonicalization(canonicalStream, IncludeComments, _inclusivePrefixes); |
| | 0 | 73 | | reader.Skip(); |
| | 0 | 74 | | reader.EndCanonicalization(); |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 0 | 78 | | XmlDictionaryWriter textWriter = XmlDictionaryWriter.CreateTextWriter(Stream.Null); |
| | 0 | 79 | | if (_inclusivePrefixes != null) |
| | | 80 | | { |
| | 0 | 81 | | textWriter.WriteStartElement("a", _reader.LookupNamespace(string.Empty)); |
| | 0 | 82 | | for (int index = 0; index < _inclusivePrefixes.Length; ++index) |
| | | 83 | | { |
| | 0 | 84 | | string namespaceUri = _reader.LookupNamespace(_inclusivePrefixes[index]); |
| | 0 | 85 | | if (namespaceUri != null) |
| | 0 | 86 | | textWriter.WriteXmlnsAttribute(_inclusivePrefixes[index], namespaceUri); |
| | | 87 | | } |
| | | 88 | | } |
| | 0 | 89 | | textWriter.StartCanonicalization(canonicalStream, IncludeComments, _inclusivePrefixes); |
| | | 90 | | //TODO check the use of wrappedreader |
| | | 91 | | // if (this.reader is WrappedReader) |
| | | 92 | | // ((WrappedReader) this.reader).XmlTokens.GetWriter().WriteTo(textWriter, new DictionaryManager()); |
| | | 93 | | // else |
| | 0 | 94 | | textWriter.WriteNode(_reader, false); |
| | 0 | 95 | | textWriter.Flush(); |
| | 0 | 96 | | textWriter.EndCanonicalization(); |
| | 0 | 97 | | if (_inclusivePrefixes != null) |
| | 0 | 98 | | textWriter.WriteEndElement(); |
| | 0 | 99 | | textWriter.Close(); |
| | | 100 | | } |
| | 0 | 101 | | if (CloseReadersAfterProcessing) |
| | 0 | 102 | | _reader.Close(); |
| | 0 | 103 | | _reader = (XmlReader)null; |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | } |